[英]How do I get an XLA add in into excel through python using win32com.client?
I have to run macros in an excel file, which can be done through python win32.我必须在excel文件中运行宏,这可以通过python win32来完成。 But I have a third party excel add-in required to run the macros.
但是我有一个运行宏所需的第三方 excel 加载项。 The excel which is opened through win32 does not have the add-ins linked.
通过 win32 打开的 excel 没有链接加载项。 The add-in is in a XLA file format.
加载项采用 XLA 文件格式。
This is my code这是我的代码
new_file_path = path to excel_file.xlsm
xl = win32com.client.Dispatch('Excel.Application')
xl.Visible = True
refprop_path = path to XLA file
xl.Workbooks.Open(refprop_path)
xl.AddIns.Add(refprop_path)
xl.Workbooks.Open(new_file_path)
xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
for i in range(0, 3):
xl.Application.Run("iter1.xlsm!Sheet1.temp_const_gauge")
xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
xl.Application.Save()
xl.Application.Quit()
The add-in as not added and I get an error.加载项未添加,我收到错误消息。
xl.AddIns.Add(refprop_path).Installed = True
File "<COMObject <unknown>>", line 3, in Add
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u'Add method of AddIns class failed', u'xlmain11.chm', 0, -2146827284), None)
I did not have to explicitly write a line to add the add in. I removed that line and the problem was solved.我不必明确地写一行来添加 add in。我删除了那一行,问题就解决了。
Working code:工作代码:
new_file_path = path to excel_file.xlsm
xl = win32com.client.Dispatch('Excel.Application')
xl.Visible = True
refprop_path = path to XLA file
xl.Workbooks.Open(refprop_path)
xl.Workbooks.Open(new_file_path)
xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
for i in range(0, 3):
xl.Application.Run("iter1.xlsm!Sheet1.temp_const_gauge")
xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
xl.Application.Save()
xl.Application.Quit()
I seems that the add-in in the XLA file can be used just by keeping it open in the background.我似乎可以通过在后台保持打开来使用 XLA 文件中的加载项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.