Is there a way to run a VBA macro on an MS Word document using Python in MacOS?

Viewed 144

Long-time listener, first-time caller.

I'm trying to find a way to run a VBA macro on a Word document using Python in MacOS. My searches so far have turned up many solutions that tick 2-3 of those boxes, but nothing that ticks all 4. The closest I have found is the below code (from this SO question):

import os
import comtypes.client

path=r"MyMacro.docm"

word=comtypes.client.CreateObject("Word.Application")
word.Documents.Open(path,ReadOnly=1)
word.Run("'MyDocument.docx'!ThisModule.ThisProcedure"))
word.Documents(1).Close(SaveChanges=0)
word.Application.Quit()
wd=0

Unfortunately, this uses comtypes, which is Windows-only.

The problem that I'm ultimately trying to solve here is applying conditional coloring to cells of a table in a Word document being generated with the docx-mailmerge package for Python on a MacOS machine. I already have a VBA macro with which this can be done manually, I'm just trying to figure out if this is something that I can automate.

0 Answers
Related