Keerthan G
Keerthan G

Reputation: 1

Python pywin32 Adding Excel Object, IconLabel not displaying

I am using pywin32 module to add an excel file as an object in another excel file, I need to get the label added to it as Europe in the object. Even after adding the parameter IconLabel in code, I don't see any caption in the object attached to Excel, can anyone please let me know what is wrong in the code?

Here is the following Code I have used:

import win32com.client as win32
import sys
Filename=str(sys.argv[1])
Sheetname=str(sys.argv[2])
Filename2=str(sys.argv[3])
xl = win32.gencache.EnsureDispatch('Excel.Application')
wb=xl.Workbooks.Open(Filename)
ws=wb.Worksheets(Sheetname)
dest_cell=ws.Range("A1")
obj = ws.OLEObjects()
obj.Add(Filename=Filename2,Link=False,DisplayAsIcon=True,IconLabel="Europe",Left=dest_cell.Left,Top=dest_cell.Top)
wb.Save()
xl.Application.Quit()


  

Upvotes: 0

Views: 260

Answers (2)

You can try with this:

import win32com.client as win32

os.system("taskkill /f /im excel.exe") #Close any excel process is opening to avoid fail

xl = win32.dynamic.Dispatch('Excel.Application')
xl.Visible = False

wb = xl.Workbooks.Open('C:\\Trial\\example.xlsx') #<<---- Change to your excel file here.
ws = wb.Worksheets('Sheet1')

dest_cell = ws.Range("B5") #<--- Add location to insert the object.
obj = ws.OLEObjects()
obj.Add(ClassType=None,
        Filename='C:\\Trial\\YourPhoto.jpg',#<<---- Change to your file need to add by Object
        Link=False, 
        DisplayAsIcon=True,
        Left=dest_cell.Left,
        Top=dest_cell.Top,
        Width=200,
        Height=100)

wb.Save()
xl.Application.Quit()

Upvotes: 0

龚运杰
龚运杰

Reputation: 1

你好,我经过测试,在Excel 2021版本的Excel测试成功: Embedded_object = worksheet.OLEObjects()

Embedded_object.Add(ClassType=None, Filename=file_location, Link=False, DisplayAsIcon=True,IconLabel=worksheet.Cells(G, 7).Value,Left=left_index,Top = top_index,IconFileName=r"C:\Windows\Installer{90160000-000F-0000-1000-0000000FF1CE}\xlicons.exe",IconIndex=0)
#IconFileName参数和时必须的,不确定值的时候,请使用Excel的录制宏功能获取,IconIndex=0是固定的

Upvotes: 0

Related Questions