Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming COM events in Python

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 normal IConnectionPoint interfaces, 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")
...
like image 358
da_m_n Avatar asked Aug 07 '09 12:08

da_m_n


People also ask

What is event function in Python?

Events are the constructs that enable a class to notify other classes when something of interest takes place. In the layman language, it is basically similar to raising a flag to signal others that something of interest has happened.

Does Python have event handling?

Introduction to Python Event HandlerAn Event Handler is a class, part of events, which simply is responsible for managing all callbacks which are to be executed when invoked. An Event is simply an action, like clicking a button, which then leads to another event, which the developer already assigns.

What is Python event driven explain keypress event with an example?

The working of Event-Driven programming depends upon the events happening in a program. Other than this, it depends upon the program's event loops that always listen to a new incoming event in the program. Once an event loop starts in the program, then only the events will decide what will execute and in which order.


1 Answers

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:

ListenerBase = win32com.client.getevents("SAPI.SpInProcRecoContext")
class Listener(ListenerBase):
    def OnRecognition(self, _1, _2, _3, Result):
        """Callback whenever something is recognized."""
        # Work with Result

    def OnHypothesis(self, _1, _2, Result):
        """Callback whenever we have a potential match."""
        # Work with Result

then later in a main loop:

    while not self.shutting_down.is_set():
        # Trigger the event handlers if we have anything.
        pythoncom.PumpWaitingMessages() 
        time.sleep(0.1) # Don't use up all our CPU checking constantly

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.

like image 174
Kiv Avatar answered Sep 20 '22 02:09

Kiv