Using Python to program MS Office macros?

Yes, absolutely. You want to use win32com module, which is part of pywin32 (get it here).

I’ve found you can really simplify Python integration by writing a macro in VBA for Python to use, and then just have Python call the macro. It will look something like this:

from win32com.client import Dispatch as comDispatch

xl = comDispatch('Excel.Application')
xl.Workbooks.Open("Macros.xls", False, True)
xl.Run("Macros.xls!Macro_1")

I’m sure there are plently of examples on SO… Like this one.

Leave a Comment