我们从Python开源项目中,提取了以下28个代码示例,用于说明如何使用win32com.client.gencache.EnsureModule()。
def TestAll(): TestWord() print "Starting Excel for Dynamic test..." xl = win32com.client.dynamic.Dispatch("Excel.Application") TextExcel(xl) try: print "Starting Excel 8 for generated excel8.py test..." mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 0, 1, 2, bForDemand=1) xl = win32com.client.Dispatch("Excel.Application") TextExcel(xl) except ImportError: print "Could not import the generated Excel 97 wrapper" try: import xl5en32 mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 9, 1, 0) xl = win32com.client.Dispatch("Excel.Application.5") print "Starting Excel 95 for makepy test..." TextExcel(xl) except ImportError: print "Could not import the generated Excel 95 wrapper"
def TestAll(): TestWord() print("Starting Excel for Dynamic test...") xl = win32com.client.dynamic.Dispatch("Excel.Application") TextExcel(xl) try: print("Starting Excel 8 for generated excel8.py test...") mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 0, 1, 2, bForDemand=1) xl = win32com.client.Dispatch("Excel.Application") TextExcel(xl) except ImportError: print("Could not import the generated Excel 97 wrapper") try: import xl5en32 mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 9, 1, 0) xl = win32com.client.Dispatch("Excel.Application.5") print("Starting Excel 95 for makepy test...") TextExcel(xl) except ImportError: print("Could not import the generated Excel 95 wrapper")
def ShowInfo(spec): if not spec: tlbSpec = selecttlb.SelectTlb(excludeFlags=selecttlb.FLAG_HIDDEN) if tlbSpec is None: return try: tlb = pythoncom.LoadRegTypeLib(tlbSpec.clsid, tlbSpec.major, tlbSpec.minor, tlbSpec.lcid) except pythoncom.com_error: # May be badly registered. sys.stderr.write("Warning - could not load registered typelib '%s'\n" % (tlbSpec.clsid)) tlb = None infos = [(tlb, tlbSpec)] else: infos = GetTypeLibsForSpec(spec) for (tlb, tlbSpec) in infos: desc = tlbSpec.desc if desc is None: if tlb is None: desc = "<Could not load typelib %s>" % (tlbSpec.dll) else: desc = tlb.GetDocumentation(-1)[0] print desc print " %s, lcid=%s, major=%s, minor=%s" % (tlbSpec.clsid, tlbSpec.lcid, tlbSpec.major, tlbSpec.minor) print " >>> # Use these commands in Python code to auto generate .py support" print " >>> from win32com.client import gencache" print " >>> gencache.EnsureModule('%s', %s, %s, %s)" % (tlbSpec.clsid, tlbSpec.lcid, tlbSpec.major, tlbSpec.minor)
def TestBuildAll(verbose = 1): num = 0 tlbInfos = selecttlb.EnumTlbs() for info in tlbInfos: if verbose: print "%s (%s)" % (info.desc, info.dll) try: makepy.GenerateFromTypeLibSpec(info) # sys.stderr.write("Attr typeflags for coclass referenced object %s=%d (%d), typekind=%d\n" % (name, refAttr.wTypeFlags, refAttr.wTypeFlags & pythoncom.TYPEFLAG_FDUAL,refAttr.typekind)) num += 1 except pythoncom.com_error, details: # Ignore these 2 errors, as the are very common and can obscure # useful warnings. if details.hresult not in [winerror.TYPE_E_CANTLOADLIBRARY, winerror.TYPE_E_LIBNOTREGISTERED]: print "** COM error on", info.desc print details except KeyboardInterrupt: print "Interrupted!" raise KeyboardInterrupt except: print "Failed:", info.desc traceback.print_exc() if makepy.bForDemandDefault: # This only builds enums etc by default - build each # interface manually tinfo = (info.clsid, info.lcid, info.major, info.minor) mod = gencache.EnsureModule(info.clsid, info.lcid, info.major, info.minor) for name in mod.NamesToIIDMap.iterkeys(): makepy.GenerateChildFromTypeLibSpec(name, tinfo) return num
def GenerateSupport(): # dao gencache.EnsureModule("{00025E01-0000-0000-C000-000000000046}", 0, 4, 0) # Access # gencache.EnsureModule("{4AFFC9A0-5F99-101B-AF4E-00AA003F0F07}", 0, 8, 0) gencache.EnsureDispatch("Access.Application")
def TestAll(): try: try: iexplore = win32com.client.dynamic.Dispatch("InternetExplorer.Application") TestExplorer(iexplore) win32api.Sleep(1000) iexplore = None # Test IE events. TestExplorerEvents() # Give IE a chance to shutdown, else it can get upset on fast machines. time.sleep(2) # Note that the TextExplorerEvents will force makepy - hence # this gencache is really no longer needed. from win32com.client import gencache gencache.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1) iexplore = win32com.client.Dispatch("InternetExplorer.Application") TestExplorer(iexplore) except pythoncom.com_error, exc: if exc.hresult!=winerror.RPC_E_DISCONNECTED: # user closed the app! raise finally: iexplore = None
def AddTypeLib(self, uuid, major, minor, flags): # Get the win32com gencache to register this library. from win32com.client import gencache gencache.EnsureModule(uuid, self.lcid, major, minor, bForDemand = 1) # This is never called by the C++ framework - it does magic. # See PyGActiveScript.cpp #def InterruptScriptThread(self, stidThread, exc_info, flags): # raise Exception("Not Implemented", scode=winerror.E_NOTIMPL)
def GetTestVideoModule(): global videoControlModule, videoControlFileName win32ui.DoWaitCursor(1) videoControlModule = gencache.EnsureModule("{05589FA0-C356-11CE-BF01-00AA0055595A}", 0, 2, 0) win32ui.DoWaitCursor(0) if videoControlModule is None: return None fnames = glob.glob(os.path.join(win32api.GetWindowsDirectory(), "*.avi")) if not fnames: print "No AVI files available in system directory" return None videoControlFileName = fnames[0] return videoControlModule
def MDITest(): calendarParentModule = gencache.EnsureModule("{8E27C92E-1264-101C-8A2F-040224009C02}", 0, 7, 0) class MyCal(activex.Control, calendarParentModule.Calendar): def OnAfterUpdate(self): print "OnAfterUpdate" def OnClick(self): print "OnClick" f = OCXFrame() f.Create(MyCal, "Calendar Test")
def GetTestVideoModule(): global videoControlModule, videoControlFileName win32ui.DoWaitCursor(1) videoControlModule = gencache.EnsureModule("{05589FA0-C356-11CE-BF01-00AA0055595A}", 0, 2, 0) win32ui.DoWaitCursor(0) if videoControlModule is None: return None fnames = glob.glob(os.path.join(win32api.GetWindowsDirectory(), "*.avi")) if not fnames: print("No AVI files available in system directory") return None videoControlFileName = fnames[0] return videoControlModule
def MDITest(): calendarParentModule = gencache.EnsureModule("{8E27C92E-1264-101C-8A2F-040224009C02}", 0, 7, 0) class MyCal(activex.Control, calendarParentModule.Calendar): def OnAfterUpdate(self): print("OnAfterUpdate") def OnClick(self): print("OnClick") f = OCXFrame() f.Create(MyCal, "Calendar Test")
def ShowInfo(spec): if not spec: tlbSpec = selecttlb.SelectTlb(excludeFlags=selecttlb.FLAG_HIDDEN) if tlbSpec is None: return try: tlb = pythoncom.LoadRegTypeLib(tlbSpec.clsid, tlbSpec.major, tlbSpec.minor, tlbSpec.lcid) except pythoncom.com_error: # May be badly registered. sys.stderr.write("Warning - could not load registered typelib '%s'\n" % (tlbSpec.clsid)) tlb = None infos = [(tlb, tlbSpec)] else: infos = GetTypeLibsForSpec(spec) for (tlb, tlbSpec) in infos: desc = tlbSpec.desc if desc is None: if tlb is None: desc = "<Could not load typelib %s>" % (tlbSpec.dll) else: desc = tlb.GetDocumentation(-1)[0] print(desc) print(" %s, lcid=%s, major=%s, minor=%s" % (tlbSpec.clsid, tlbSpec.lcid, tlbSpec.major, tlbSpec.minor)) print(" >>> # Use these commands in Python code to auto generate .py support") print(" >>> from win32com.client import gencache") print(" >>> gencache.EnsureModule('%s', %s, %s, %s)" % (tlbSpec.clsid, tlbSpec.lcid, tlbSpec.major, tlbSpec.minor))
def TestAll(): try: try: iexplore = win32com.client.dynamic.Dispatch("InternetExplorer.Application") TestExplorer(iexplore) win32api.Sleep(1000) iexplore = None # Test IE events. TestExplorerEvents() # Give IE a chance to shutdown, else it can get upset on fast machines. time.sleep(2) # Note that the TextExplorerEvents will force makepy - hence # this gencache is really no longer needed. from win32com.client import gencache gencache.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1) iexplore = win32com.client.Dispatch("InternetExplorer.Application") TestExplorer(iexplore) except pythoncom.com_error as exc: if exc.hresult!=winerror.RPC_E_DISCONNECTED: # user closed the app! raise finally: iexplore = None
def TestWord(): # Try and load the object exposed by Word 8 # Office 97 - _totally_ different object model! try: # NOTE - using "client.Dispatch" would return an msword8.py instance! print("Starting Word 8 for dynamic test") word = win32com.client.dynamic.Dispatch("Word.Application") TestWord8(word) word = None # Now we will test Dispatch without the new "lazy" capabilities print("Starting Word 8 for non-lazy dynamic test") dispatch = win32com.client.dynamic._GetGoodDispatch("Word.Application") typeinfo = dispatch.GetTypeInfo() attr = typeinfo.GetTypeAttr() olerepr = win32com.client.build.DispatchItem(typeinfo, attr, None, 0) word = win32com.client.dynamic.CDispatch(dispatch, olerepr) dispatch = typeinfo = attr = olerepr = None TestWord8(word) except pythoncom.com_error: print("Starting Word 7 for dynamic test") word = win32com.client.Dispatch("Word.Basic") TestWord7(word) try: print("Starting MSWord for generated test") # Typelib, lcid, major and minor for the typelib try: o = gencache.EnsureModule("{00020905-0000-0000-C000-000000000046}", 1033, 8, 0, bForDemand=1) except TypeError: o = gencache.EnsureModule("{00020905-0000-0000-C000-000000000046}", 1033, 8, 0) if o is None : raise ImportError, "Can not load the Word8 typelibrary." word = win32com.client.Dispatch("Word.Application.8") TestWord8(word) except ImportError as details: print("Can not test MSWord8 -", details)
def TestAll(): try: TestWord() print("Starting Excel for Dynamic test...") xl = win32com.client.dynamic.Dispatch("Excel.Application") TextExcel(xl) try: print("Starting Excel 8 for generated excel8.py test...") try: mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 0, 1, 2, bForDemand=1) except TypeError: mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 0, 1, 2) xl = win32com.client.Dispatch("Excel.Application") TextExcel(xl) except ImportError: print("Could not import the generated Excel 97 wrapper") try: import xl5en32 mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 9, 1, 0) xl = win32com.client.Dispatch("Excel.Application.5") print("Starting Excel 95 for makepy test...") TextExcel(xl) except ImportError: print("Could not import the generated Excel 95 wrapper") except KeyboardInterrupt: print("*** Interrupted MSOffice test ***") except: traceback.print_exc()
def GetTestCalendarClass(): global calendarParentModule win32ui.DoWaitCursor(1) calendarParentModule = gencache.EnsureModule("{8E27C92E-1264-101C-8A2F-040224009C02}", 0, 7, 0) win32ui.DoWaitCursor(0) if calendarParentModule is None: return None class TestCalDialog(dialog.Dialog): def OnInitDialog(self): class MyCal(activex.Control, calendarParentModule.Calendar): def OnAfterUpdate(self): print "OnAfterUpdate" def OnClick(self): print "OnClick" def OnDblClick(self): print "OnDblClick" def OnKeyDown(self, KeyCode, Shift): print "OnKeyDown", KeyCode, Shift def OnKeyPress(self, KeyAscii): print "OnKeyPress", KeyAscii def OnKeyUp(self, KeyCode, Shift): print "OnKeyUp", KeyCode, Shift def OnBeforeUpdate(self, Cancel): print "OnBeforeUpdate", Cancel def OnNewMonth(self): print "OnNewMonth" def OnNewYear(self): print "OnNewYear" rc = dialog.Dialog.OnInitDialog(self) self.olectl = MyCal() try: self.olectl.CreateControl("OCX", win32con.WS_TABSTOP | win32con.WS_VISIBLE, (7,43,500,300), self._obj_, 131) except win32ui.error: self.MessageBox("The Calendar Control could not be created") self.olectl = None self.EndDialog(win32con.IDCANCEL) return rc def OnOK(self): self.olectl.AboutBox() return TestCalDialog #################################### # # Video Control #
def GetTestCalendarClass(): global calendarParentModule win32ui.DoWaitCursor(1) calendarParentModule = gencache.EnsureModule("{8E27C92E-1264-101C-8A2F-040224009C02}", 0, 7, 0) win32ui.DoWaitCursor(0) if calendarParentModule is None: return None class TestCalDialog(dialog.Dialog): def OnInitDialog(self): class MyCal(activex.Control, calendarParentModule.Calendar): def OnAfterUpdate(self): print("OnAfterUpdate") def OnClick(self): print("OnClick") def OnDblClick(self): print("OnDblClick") def OnKeyDown(self, KeyCode, Shift): print("OnKeyDown", KeyCode, Shift) def OnKeyPress(self, KeyAscii): print("OnKeyPress", KeyAscii) def OnKeyUp(self, KeyCode, Shift): print("OnKeyUp", KeyCode, Shift) def OnBeforeUpdate(self, Cancel): print("OnBeforeUpdate", Cancel) def OnNewMonth(self): print("OnNewMonth") def OnNewYear(self): print("OnNewYear") rc = dialog.Dialog.OnInitDialog(self) self.olectl = MyCal() try: self.olectl.CreateControl("OCX", win32con.WS_TABSTOP | win32con.WS_VISIBLE, (7,43,500,300), self._obj_, 131) except win32ui.error: self.MessageBox("The Calendar Control could not be created") self.olectl = None self.EndDialog(win32con.IDCANCEL) return rc def OnOK(self): self.olectl.AboutBox() return TestCalDialog #################################### # # Video Control #