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

标签 python excel vba python-2.7 pywin32

我必须在 Excel 文件中运行宏,这可以通过 python win32 来完成。但我需要第三方 Excel 插件来运行宏。通过win32打开的excel没有链接的加载项。该加载项采用 XLA 文件格式。

这是我的代码

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()

加载项未添加,我收到错误。

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)

最佳答案

我不必显式地编写一行来添加加载项。我删除了该行,问题就解决了。

工作代码:

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()

我似乎只需在后台保持打开状态即可使用 XLA 文件中的加载项。

关于python - 如何使用 win32com.client 通过 python 将 XLA 添加到 Excel 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54071902/

相关文章:

mysql - RecordSet 上的 GetRows 不会存储 Access DB 中的文本列

excel - 如何轻松更改 Excel 中文件的硬编码链接?

excel - 在行中查找文本,然后在该列中查找文本

vba - Excel VBA 使用 Workbook.Open 并显示 Dir(Directory) 的结果

python - 如何查找特定文本并打印其后的接下来的 2 个单词

python - 如何运行kivy程序?

python - 具有多个参数的 Django 动态 url

python - 使用带有 mysql unix 时间戳的 sqlalchemy 定义表

excel - 在 Visual Studio 中执行的包在没有有用错误的情况下取消

Word 中的 Excel 应用程序未关闭