pyvot: can I run Excel VBA macros from python scri

2019-02-20 17:30发布

Say I have loaded Report WW26blueprint_with_chart_330.xlsm in the report object with the following code (the print.range is used here to test that the workbook is loaded):

import xl
report = xl.Workbook("Report WW26blueprint_with_chart_330.xlsm")
print report
print(report.range("A1:E50").get())

From the pyvot documentation I get that:

Pyvot is written on top of Excel’s COM API. You can always get the underlying COM object and make COM calls yourself

My Excel file has the following macros:

Macro1 
Масго2 
Macro7  

Can I run any of the above macros directly from the python script using python/pyvot?

2条回答
在下西门庆
2楼-- · 2019-02-20 17:41

Didn't manage to do it with pyvot but can be done using pywin32 as following:

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="C:\Users\florinescu_eduard\Downloads\\Report WW26blueprint_with_chart_330.xlsm",ReadOnly=1)
xl.Application.Run("Macro1")
xl.Workbooks(1).Close(SaveChanges=1)
xl.Application.Quit()
xl=0
查看更多
时光不老,我们不散
3楼-- · 2019-02-20 17:42

This did it for me:

report.xlWorkbook.Application.Run('Macro1')
查看更多
登录 后发表回答