简体   繁体   English

如何使用 win32com.client 通过 python 将 XLA 添加到 excel 中?

[英]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.

相关问题 如何使用 python win32com.client 在 Excel 中添加折线? - How to Add polyline in Excel with python win32com.client? 如何关闭另一个应用程序打开的 Excel 文件? 将 python 与 win32com.client 一起使用 - How to close an Excel file opened by another application? Using python with win32com.client 如何使用Python和win32com.client在PowerPoint中操作形状(颜色)? - How can I manipulate shapes (colors) in PowerPoint using Python and win32com.client? 如何在python中使用win32com.client将文档另存为.psb文件? - How can I save as the document as .psb file using win32com.client in python? Python win32com.client Excel xlTopCount - Python win32com.client Excel xlTopCount Python win32com.client和outlook - Python win32com.client and outlook Python win32com.client和“with”语句 - Python win32com.client and “with” statement 无法在 python 3.7 中使用 win32com.client 删除工作簿中的 Excel 工作表 - Not able to delete an excel sheet in a workbook using win32com.client in python 3.7 使用任务计划程序 (Python) 通过 win32com.client 运行 Outlook - Running Outlook through win32com.client using Task Scheduler (Python) 如何选择win32com.client必须在python中使用的excel版本? - How to choose the version of excel which win32com.client has to use in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM