通过Python运行Excelmacros?

我试图通过python运行macros,但我不知道如何得到它的工作…

到目前为止我有下面的代码,但它不工作。

import win32com.client xl=win32com.client.Dispatch("Excel.Application") xl.Workbooks.Open(Filename="C:\test.xlsm",ReadOnly=1) xl.Application.Run("macrohere") xl.Workbooks(1).Close(SaveChanges=0) xl.Application.Quit() xl=0 

我得到以下回溯:

 Traceback (most recent call last): File "C:\test.py", line 4, in <module> xl.Application.Run("macrohere") File "<COMObject <unknown>>", line 14, in Run File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 282, in _ApplyTypes_ result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args) com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u"Cannot run the macro 'macrohere'. The macro may not be available in this workbook or all macros may be disabled.", u'xlmain11.chm', 0, -2146827284), None) 

编辑

 import win32com.client xl=win32com.client.Dispatch("Excel.Application") xl.Workbooks.Open(Filename="C:\test.xlsm",ReadOnly=1) try: xl.Application.Run("test.xlsm!testmacro.testmacro") # It does run like this... but we get the following error: # Traceback (most recent call last): # File "C:\test.py", line 7, in <module> # xl.Workbooks(1).Close(SaveChanges=0) # File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 192, in __call__ # return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.defaultDispatchName,None) # com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None) except: # Except isn't catching the above error... :( xl.Workbooks(1).Close(SaveChanges=0) xl.Application.Quit() xl=0 

我会除了错误的是要处理您所调用的macros,请尝试以下代码:

 import os import win32com.client if os.path.exists("excelsheet.xlsm"): xl=win32com.client.Dispatch("Excel.Application") xl.Workbooks.Open(Filename="C:\Full Location\To\excelsheet.xlsm", ReadOnly=1) xl.Application.Run("excelsheet.xlsm!modulename.macroname") ## xl.Application.Save() # if you want to save then uncomment this line and change delete the ", ReadOnly=1" part from the open function. xl.Application.Quit() # Comment this out if your excel script closes del xl 

希望有所帮助
所有最好的SMNALLY

我怀疑你没有授权你的Excel安装从一个自动化的Excel中运行macros。 安装时默认是安全防护。 要改变这个:

  1. 文件>选项>信任中心
  2. 点击信任中心设置…button
  3. macros设置>检查启用所有macros

嗯,我有一些麻烦,这一部分(是的还是xD):

xl.Application.Run("excelsheet.xlsm!macroname.macroname")

因此,我不经常使用Excel(与vb或macros相同,但我需要它与python使用femap),所以我finfinded解决它检查macros列表:开发人员 – >macros:我看到:这个macroname.macroname应该是sheet_name.macroname就像在“macros”列表中。

(我花了30分钟到1小时的时间试图解决这个问题,所以对于像我这样的新手来说,这可能会有所帮助)xD

我对SMNALLY的代码做了一些修改,所以可以在Python 3.5.2中运行。 这是我的结果:

 def runMacro(): if os.path.exists("C:\\Users\\Dev\\Desktop\\Development\\completed_apps\\My_Macr_Generates_Data.xlsm"): # DispatchEx is required in the newest versions of Python. excel_macro = wincl.DispatchEx("Excel.application") excel_path = os.path.expanduser("C:\\Users\\Dev\\Desktop\\Development\\completed_apps\\My_Macr_Generates_Data.xlsm") workbook = excel_macro.Workbooks.Open(Filename = excel_path, ReadOnly =1) excel_macro.Application.Run\ ("ThisWorkbook.Template2G") #Save the results in case you have generated data workbook.Save() excel_macro.Application.Quit() del excel_macro