Running an Excel macro via Python?
I would expect the error is to do with the macro you're calling, try the following bit of code:
Code
import os, os.pathimport win32com.clientif os.path.exists("excelsheet.xlsm"): xl=win32com.client.Dispatch("Excel.Application") xl.Workbooks.Open(os.path.abspath("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
I did some modification to the SMNALLY's code so it can run in Python 3.5.2. This is my result:
#Import the following library to make use of the DispatchEx to run the macroimport win32com.client as wincldef 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
Just a quick note with a xlsm with spaces.
file = 'file with spaces.xlsm'excel_macro.Application.Run('\'' + file + '\'' + "!Module1.Macro1")