Environment
OS: win 7
Excel 2016 64bit
Python 3.7.8
xlwings 0.30.5
Issue
When I use the following code to add the PDF file into a Excel, the IconFileName and IconLabel parameters have no effect at all and the embedded PDF becomes a very small dot and if enlarging the dot, I can see a white rectangle without any lable text.
Also it will get an error at the line saving excel to new path.
obj = sheet1.api.OLEObjects()
obj.Add(ClassType=None, Filename=os.path.abspath(path), Link=False, DisplayAsIcon=True, IconFileName='test.ico', IconLabel='test')
sheet1.save(os.path.abspath(new_path))
If using DisplayAsIcon= False, the PDF will be well embedded, but not shown as an icon. It worked and could be save as without any error.
obj.Add(ClassType=None, Filename=os.path.abspath(path), Link=False, DisplayAsIcon=False)
Is anyone who has the same issue with the DisplayAsIcon issue? Thanks very much.
Traceback
File "C:\Users\account\AppData\Roaming\Python\Python37\site-packages\xlwings\main.py", line 1163, in save
self.impl.save(path, password=password)
File "C:\Users\account\AppData\Roaming\Python\Python37\site-packages\xlwings_xlwindows.py", line 895, in save
os.path.realpath(path), FileFormat=file_format, Password=password
File "C:\Users\account\AppData\Roaming\Python\Python37\site-packages\xlwings_xlwindows.py", line 122, in call
v = self.__method(*args, **kwargs)
File "C:\Users\account\AppData\Local\Temp\gen_py\3.7\00020813-0000-0000-C000-000000000046x0x1x9.py", line 46616, in SaveAs
, Local, WorkIdentity)
pywintypes.com_error: (-2147352567, '发生意外。', (0, 'Microsoft Excel', '文档未保存。', 'xlmain11.chm', 0, -2146827284), None)
I tried to do the same in another computer with office 2021 and Win10. It will not be an error and could be saved. The embedded PDF will be shown as an icon with default PDF icon and file name no matter I choose DisplayAsIcon=True or False. And the IconFileName and IconLabel parameters also have no effect at all.
I need to add PDF with showing as icon in win7 Excel2016 in the specified computer and stuck several weeks for this problem.
Is there any idea to solve it?
It will be of great help if any idea.
If DisplayAsIcon
is enabled
DisplayAsIcon=True
then you need need to include IconIndex
, e.g.
IconIndex=0
The following code example works fine for me.
Since Filename
is set ClassType
can be omitted or set to None as you have it.
wb.sheets['Sheet1'].api.OLEObjects().Add(
# ClassType="Acrobat.Document.DC",
# ClassType=None,
Filename=r"C:\Temp\Adobe.pdf",
Link=False,
DisplayAsIcon=True,
IconFileName=r"C:\Temp\test.ico",
IconIndex=0,
IconLabel="test"
)