使用Win32 COM python库将VBA宏应用于Excel文件

使用Win32 COM python库将VBA宏应用于Excel文件,python,excel,win32com,Python,Excel,Win32com,我正在尝试使用python和win32com库应用VBA宏,想法是创建一个固定的excel文件,存储一个宏,然后运行它。我的想法来自这里: 当脚本必须使用xlsm文件并使用Application.Run()运行宏时,就会出现问题。我的代码是: import pandas as pd import win32com.client import os #creating the dataframe df = df1=pd.read_excel ("normal_excel_file.xlsx"

我正在尝试使用python和win32com库应用VBA宏,想法是创建一个固定的excel文件,存储一个宏,然后运行它。我的想法来自这里:

当脚本必须使用xlsm文件并使用Application.Run()运行宏时,就会出现问题。我的代码是:

import pandas as pd 
import win32com.client 
import os

#creating the dataframe
df = df1=pd.read_excel ("normal_excel_file.xlsx", index_col=0)
filename = "normal_excel_file.xlsx"
writer = pd.ExcelWriter(filename, engine='xlsxwriter')
df.to_excel(writer, sheet_name='data', index=False)

#copiying and renaming the file to xlsm
shutil.copy("normal_excel_file.xlsx", "(1)normal_excel_file.xlsx")
os.rename("(1)normal_excel_file.xlsx", "macros_excel_file.xlsm")

#adding the macro to the workbook
filename_macro = r"C:\Users\John\Desktop\Python_scripts\Running VBA Macro\macros_excel_file.xlsm"
workbook = writer.book
workbook.filename = filename_macro
workbook.add_vba_project('vbaProject.bin')
writer.save()
冲突部分:

 if os.path.exists(filename_macro):
        xl = win32com.client.Dispatch('Excel.Application')
        xl.Workbooks.Open(Filename = filename_macro, ReadOnly=1)

        #assuming that there is only one macro, and is stored as "ThisWorkbook.Macro1" in the file

        xl.Application.Run("ThisWorkbook.Macro1") #I also try using only "Macro1" and the whole path of the file
        xl.Application.Quit()
        del xl
我得到了下一个错误。首先显示错误消息:

com_error: (-2147352567, 'An exception occurred.', (0, 'Microsoft Excel', 'The "ThisWorkbook.Macro1" macro cannot be executed. The macro may not be available in this book or all of them may have been disabled. the macros. ',' xlmain11.chm ', 0, -2146827284), None)
以及整个错误文本:

com_error                                 Traceback (most recent call last)
<ipython-input-16-6e91b8d2f622> in <module>
      3     xl = win32com.client.Dispatch('Excel.Application')
      4     xl.Workbooks.Open(Filename = filename_macro, ReadOnly=1)
----> 5     xl.Application.Run("ThisWorkbook.Macro1")
      6     xl.Application.Quit()
      7     del xl

~\Anaconda3\lib\site-packages\win32com\client\dynamic.py in Run(self, Macro, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, Arg12, Arg13, Arg14, Arg15, Arg16, Arg17, Arg18, Arg19, Arg20, Arg21, Arg22, Arg23, Arg24, Arg25, Arg26, Arg27, Arg28, Arg29, Arg30)

~\Anaconda3\lib\site-packages\win32com\client\dynamic.py in _ApplyTypes_(self, dispid, wFlags, retType, argTypes, user, resultCLSID, *args)
    285 
    286         def _ApplyTypes_(self, dispid, wFlags, retType, argTypes, user, resultCLSID, *args):
--> 287                 result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
    288                 return self._get_good_object_(result, user, resultCLSID)
    289 

com_error: (-2147352567, 'An exception occurred.', (0, 'Microsoft Excel', 'The "ThisWorkbook.Macro1" macro cannot be executed. The macro may not be available in this book or all of them may have been disabled. the macros. ',' xlmain11.chm ', 0, -2146827284), None)
com_错误回溯(最近一次调用)
在里面
3 xl=win32com.client.Dispatch('Excel.Application')
4 xl.Workbooks.Open(Filename=Filename\u宏,只读=1)
---->5.xl.Application.Run(“ThisWorkbook.Macro1”)
6.xl.Application.Quit()
7德尔xl
运行中的~\Anaconda3\lib\site packages\win32com\client\dynamic.py(self、宏、Arg1、Arg2、Arg3、Arg4、Arg5、Arg6、Arg7、Arg8、Arg9、Arg10、Arg11、Arg12、Arg13、Arg14、Arg15、Arg16、Arg17、Arg18、Arg19、Arg20、Arg21、Arg22、Arg23、Arg24、Arg25、Arg26、Arg27、Arg28、Arg29、Arg30)
应用程序类型中的~\Anaconda3\lib\site packages\win32com\client\dynamic.py(self、dispid、wFlags、retType、argTypes、user、resultcsid、*args)
285
286定义应用程序类型(self、dispid、wFlags、retType、argTypes、user、resultcsid、*args):
-->287 result=self.\u oleobj.\u.InvokeTypes(*(dispid、LCID、wFlags、retType、argTypes)+args)
288返回self.\u获取\u良好\u对象\u(结果、用户、结果csid)
289
com_错误:(-2147352567,‘发生异常’,(0,‘Microsoft Excel’,“ThisWorkbook.Macro1”宏无法执行。该宏可能在本书中不可用,或者所有宏都已被禁用。宏。“,”xlmain11.chm',0,-2146827284),无)
我转到Microsoft Office中的信任中心,并允许所有宏类型()。但错误仍在继续

如果有人知道如何修复它,那就太棒了


感谢大家

,因为
.xlsx
.xlsm
是基本不同类型的二进制文件,您不能简单地使用以下行复制和重命名:

shutil.copy("normal_excel_file.xlsx", "(1)normal_excel_file.xlsx")
os.rename("(1)normal_excel_file.xlsx", "macros_excel_file.xlsm")
(奇怪的是,您的教程链接没有显示Pandas生成的
.xlsx
文件如何成为后续附加宏步骤中的
.xlsm
。)

相反,请使用Excel writer对象迁移宏并另存为
.xlsm
。另外,在writer对象上使用
上下文管理器可以在处理后有效地关闭i/o对象

# creating the dataframe
path = "C:\Users\John\Desktop\Python_scripts\Running VBA Macro"
filename = "normal_excel_file.xlsx"
filename_macro = "macros_excel_file.xlsm"

df = pd.read_excel(filename, index_col=0)
with pd.ExcelWriter(filename, engine='xlsxwriter') as writer:
    df.to_excel(writer, sheet_name='data', index=False)
    workbook = writer.book
    workbook.filename = filename_macro
    workbook.add_vba_project('vbaProject.bin')
    writer.save()

# RUN MACRO
if os.path.exists(os.path.join(path, filename_macro)):
    wb = xl.Workbooks.Open(Filename = filename_macro, ReadOnly=1)
    xl.Application.Run("ThisWorkbook.Macro1")
    xl.Application.Quit()

如果在Excel中以VBA开发人员模式打开创建的xlsm文件,是否会出现错误?如果没有,是否有一个名为Macro1的宏,工作簿对象是否名为ThisWorkbook?宏做了什么,使您无法编写Python win32com函数?hi@jmcnamara,当我尝试访问xlsm文件时,显示以下消息///“Excel无法打开文件'filename.xlsm',因为文件格式或文件扩展名无效。验证文件是否已损坏,以及文件扩展名是否与文件格式匹配。“///当返回并将文件重新转换为xlsx时,我可以正常打开它,但由于是xlsx格式,我无法检查Macros@Parfait例如,Excel中数据透视表的下拉单元格或拖放构造系统的可能性。我这样做是为了向非技术人员报告Excel对象模型的任何内容都可以用类似Python的COM连接语言处理。请记住,VBA还建立COM连接,与Excel无关。在“工具\引用”下,首先选中VBA对象。因此,您应该能够翻译甚至避免
.xlsm
.awesome的安全问题,这也是有意义的。谢谢。有一件事,当我试图运行您的代码时,我在“#将宏添加到工作簿”部分中遇到以下错误///引擎“xlsxwriter”的扩展名无效:'xlsm'//。我将在一天结束后的几个小时内对此进行调查,但可能你可以说明这一点,显然你根本不需要
shutil
,因此
SaveAs
。见我的编辑杀手。它起作用了。我非常感激。你现在是我的英雄。谢谢