[python-win32] [Help] add all event definitions programmatically to event handler class
ng khanh
cnkhanh1986 at gmail.com
Wed Jan 23 07:16:18 EST 2019
Hi,
I tried the basic example for win32com.client to just record how many times
events have been fired? I want to test which event types would be fired by
addition to self.seen_events dictionary. self.event_methods_hash get all
available events definition name ('OnWorkbookOpen',' OnWorkbookAfterSave',
etc..). While all user_event_class definitions do a similar task I wonder
how to add all available definitions programmatically?
class ExcelEvents: ##
def __init__(self):
self.seen_events = {}
self.event_methods_hash = self._dispid_to_func_
def init_events(self,key_):
if key_ not in self.seen_events.keys():
self.seen_events[key_] = 0
else:
self.seen_events[key_] += 1
def OnWorkbookOpen(self, *args):
key_ = "OnWorkbookOpen"
self.init_events(key_)
def OnWorkbookAfterSave(self,*args):
key_ = "OnWorkbookAfterSave"
self.init_events(key_)
event_names = ["OnWorkbookOpen"]
wait_time = 1
import win32com.client as win32
excel # => <win32com.gen_py.Microsoft Excel 16.0 Object
Library._Application instance at 0x1550605301240>
excel.Visible = True
excel_events = win32.WithEvents(excel, ExcelEvents)
f_path_out = 'HHW_last_out.xlsx'
try:
book = excel.Workbooks.Open(f_path_out)
book_autorecover_status = book.EnableAutoRecover
book.EnableAutoRecover = False
book.Save()
book.EnableAutoRecover = book_autorecover_status
except Exception as e:
print("Something Wrong")
print(e)
else:
print("Finished Properly")
print(excel_events.seen_events)
finally:
excel_events.close()
Thanks
Khanh Ng
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20190123/fa6e7a0d/attachment-0001.html>
More information about the python-win32
mailing list