I don't have automated Excel, but I'm using some kind of code from the Microsoft Speech API , which might be the same for you to get 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
and then in the main loop:
while not self.shutting_down.is_set():
Edit for more details on the main loop:
When something happens, the callback is not called immediately; instead, you should call PumpWaitingMessages (), which checks to see if there are any events, and then call the appropriate callback.
If you want to do something else while this happens, you need to run the loop in a separate thread (see the streaming module); otherwise it may just sit at the bottom of your script. In my example, I ran it in a separate thread, because I also had a graphical interface; the shutting_down variable is threading.Event, which you can use to stop the loop thread.
Kiv
source share