I am trying to do a sample app in python which uses some COM objects. I’ve read the famous chapter 12 from Python Programing on Win32 but regarding this issue it only states:
All event handling is done using
normalIConnectionPointinterfaces,
and although beyond the scope of this
book, is fully supported by the
standard Python COM framework.
Can anybody shed some light on this? I’d need a simple starter sample. Something like adding code to this sample to catch the OnActivate event for the spreadsheet
import win32com.client
xl = win32com.client.Dispatch("Excel.Application")
...
I haven’t automated Excel, but I’m using some code from Microsoft’s Speech API that may be similar enough to get you started:
then later in a main loop:
Edit for more detail on the main loop:
When something happens, the callback doesn’t get called immediately; instead you have to call PumpWaitingMessages(), which checks if there are any events waiting and then calls the appropriate callback.
If you want to do something else while this is happening, you’ll have to run the loop in a separate thread (see the threading module); otherwise it can just sit at the bottom of your script. In my example I was running it in a separate thread because I also had a GUI running; the shutting_down variable is a threading.Event you can use to tell the looping thread to stop.