0

In python 3.6 I'm using an COM interface to communicate with Excel and Word, in this case Word for automated reporting as the data processing is done in python.

I don't know how python can get the members of such a COM object similar to the use of the dir() function. (Previously using Matlab, i would use the .get or .invoke methods to get this)

So the code would be:

def wordOpen(wordfile):
    pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)    
    wApp = win32com.client.DispatchEx('Word.Application')
    wDoc = wApp.Documents.Open(wordfile)
    wApp.Visible = 1
    wApp.Activate
    wRange = wDoc.Content
    return wApp, wDoc, wRange

wApp, wDoc, wRange = wordOpen(wordfile)

dir(wDoc)

.. which does not provide me the list of methods and properties of the Word document object (similar for wApp and wRange).

Similarly I've tried inspect.getmembers(wDoc) but this also does not provide the list of methods/properties that I'm looking for.

For the same thing when communicating with Excel, I used to go into the VBA editor and get a list there, but is there any method to do this from the IPython console directly?

Lendicap
  • 21
  • 3

1 Answers1

0

Did some more searching and figured it out myself. The above example uses 'dynamic dispatching' and we want to use 'static dispatching' instead. That does not change the code, it only entails a one-off action.

Dynamic is a quick-and-dirty way to create these objects, and then python knows nothing about the type of object. Static means that you have to run makepy.py from the command line to create a type library (in this case for Word objects), and after that has been done once, python knows all the info about the object every next time it launches the COM object.

The procedure is very simple and comparable to the VBA action of adding References to your project.

Lendicap
  • 21
  • 3