# NOTE: If the AddIn fails with an error, Outlook will re-register # the addin to not automatically load next time Outlook starts. To # correct this, simply re-register the addin (see above) from win32com import universal from win32com.server.exception import COMException from win32com.client import gencache, DispatchWithEvents import winerror import pythoncom from win32com.client import constants import sys # Support for COM objects we use. gencache.EnsureModule('{00062FFF-0000-0000-C000-000000000046}', 0, 9, 0, bForDemand=True) # Outlook 9 gencache.EnsureModule('{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}', 0, 2, 1, bForDemand=True) # Office 9 # The TLB defining the interfaces we implement universal.RegisterInterfaces('{AC0714F2-3D04-11D1-AE7D-00A0C90F26F4}', 0, 1, 0, ["_IDTExtensibility2"]) class ButtonEvent: def OnClick(self, button, cancel):
# This demo uses the IE4 Web Browser control. # It catches an "OnNavigate" event, and updates the frame title. # (event stuff by Neil Hodgson) import win32ui, win32con, win32api, regutil from pywin.mfc import window, activex from win32com.client import gencache import sys WebBrowserModule = gencache.EnsureModule( "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1) if WebBrowserModule is None: raise ImportError("IE4 does not appear to be installed.") class MyWebBrowser(activex.Control, WebBrowserModule.WebBrowser): def OnBeforeNavigate2(self, pDisp, URL, Flags, TargetFrameName, PostData, Headers, Cancel): self.GetParent().OnNavigate(URL) #print "BeforeNavigate2", pDisp, URL, Flags, TargetFrameName, PostData, Headers, Cancel class BrowserFrame(window.MDIChildWnd): def __init__(self, url=None): if url is None: self.url = regutil.GetRegisteredHelpFile( "Main Python Documentation") if self.url is None: self.url = "http://www.python.org" else:
error = Exception # This test uses a Python implemented COM server - ensure correctly registered. RegisterPythonServer( os.path.join( os.path.dirname(__file__), '..', "servers", "test_pycomtest.py"), "Python.Test.PyCOMTest") from win32com.client import gencache try: gencache.EnsureModule('{6BCDCB60-5605-11D0-AE5F-CADD4C000000}', 0, 1, 1) except pythoncom.com_error: print("The PyCOMTest module can not be located or generated.") print(importMsg) raise RuntimeError(importMsg) # We had a bg where RegisterInterfaces would fail if gencache had # already been run - exercise that here from win32com import universal universal.RegisterInterfaces('{6BCDCB60-5605-11D0-AE5F-CADD4C000000}', 0, 1, 1) verbose = 0 # convert a normal int to a long int - used to avoid, eg, '1L' for py3k
# Sample that uses the mscomm OCX to talk to a serial # device. # Very simple - queries a modem for ATI responses import win32ui, win32uiole import win32con from pywin.mfc import dialog, activex from win32com.client import gencache import pythoncom SERIAL_SETTINGS = '19200,n,8,1' SERIAL_PORT = 2 win32ui.DoWaitCursor(1) serialModule = gencache.EnsureModule("{648A5603-2C6E-101B-82B6-000000000014}", 0, 1, 1) win32ui.DoWaitCursor(0) if serialModule is None: raise ImportError( "MS COMM Control does not appear to be installed on the PC") def MakeDlgTemplate(): style = win32con.DS_MODALFRAME | win32con.WS_POPUP \ | win32con.WS_VISIBLE | win32con.WS_CAPTION \ | win32con.WS_SYSMENU | win32con.DS_SETFONT cs = win32con.WS_CHILD | win32con.WS_VISIBLE dlg = [ [ "Very Basic Terminal", (0, 0, 350, 180), style, None, (8, "MS Sans Serif")
# coding='utf-8' import os, sys reload(sys) sys.setdefaultencoding('utf8') from win32com.client import Dispatch, constants, gencache input_f = 'E:\\WorkSpace\\gloves_test\\Program\\Ui\\11.docx' output = 'E:\\WorkSpace\\gloves_test\\Program\\Ui\\11.pdf' print 'input file', input print 'output file', output # enable python COM support for Word 2007 # this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library" gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4) w = Dispatch("Word.Application") if True: print input_f doc = w.Documents.Open(input_f, ReadOnly=1) doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF, \ Item=constants.wdExportDocumentWithMarkup, CreateBookmarks=constants.wdExportCreateHeadingBookmarks) try: pass except: print ' exception' finally: w.Quit(constants.wdDoNotSaveChanges) if os.path.isfile(output): print 'translate success'
L1.stoplistening() assert not L1.islistening() speech.stoplistening() """ from win32com.client import constants as _constants import win32com.client import pythoncom import time import threading # Make sure that we've got our COM wrappers generated. from win32com.client import gencache gencache.EnsureModule('{C866CA3A-32F7-11D2-9602-00C04F8EE628}', 0, 5, 0) _voice = win32com.client.Dispatch("SAPI.SpVoice") _recognizer = win32com.client.Dispatch("SAPI.SpSharedRecognizer") _listeners = [] _handlerqueue = [] _eventthread = None class Listener(object): """Listens for speech and calls a callback on a separate thread.""" _all = set() def __init__(self, context, grammar, callback): """
def __init__(self, com_module): gencache.EnsureModule("859343F1-08FD-11D4-8231-00105A7C4F8C", 0, 1, 0) gencache.EnsureModule("9C31B76A-7189-49A3-9781-3C6DD6ED5AD3", 0, 1, 0) gencache.EnsureModule("1F7D5E5A-05AB-4236-B6F3-3D383B09203A", 0, 1, 0) gencache.EnsureModule("2DA9C35C-FE59-4A32-A942-325EE8A6F659", 0, 1, 0) self.com = Dispatch(com_module)
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)
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")
from win32com.client import gencache from win32com.server.exception import COMException # Typelib info for version 10 - aka Excel XP. # This is the minimum version of excel that we can work with as this is when # Microsoft introduced these interfaces. EXCEL_TLB_GUID = '{00020813-0000-0000-C000-000000000046}' # LIBID_Excel EXCEL_TLB_LCID = 0 EXCEL_TLB_MAJOR = 1 EXCEL_TLB_MINOR = 4 IID_RTDUpdateEvent = '{A43788C1-D91B-11D3-8F39-00C04F3651B8}' # Import the excel typelib to make sure we've got early-binding going on. # The "ByRef" parameters we use later won't work without this. gencache.EnsureModule(EXCEL_TLB_GUID, EXCEL_TLB_LCID, \ EXCEL_TLB_MAJOR, EXCEL_TLB_MINOR) # Tell pywin to import these extra interfaces. # -- # QUESTION: Why? The interfaces seem to descend from IDispatch, so # I'd have thought, for example, calling callback.UpdateNotify() (on the # IRTDUpdateEvent callback excel gives us) would work without molestation. # But the callback needs to be cast to a "real" IRTDUpdateEvent type. Hmm... # This is where my small knowledge of the pywin framework / COM gets hazy. # -- # Again, we feed in the Excel typelib as the source of these interfaces. universal.RegisterInterfaces(EXCEL_TLB_GUID, EXCEL_TLB_LCID, EXCEL_TLB_MAJOR, EXCEL_TLB_MINOR, ['IRtdServer','IRTDUpdateEvent']) class RTDServer(object):
# -*- coding: utf-8 -*- #https://forum.ascon.ru/index.php/topic,32760.0.html import os import pythoncom from win32com.client import Dispatch, gencache # Подключим описание интерфейсов API5 api5 = gencache.EnsureModule("{0422828C-F174-495E-AC5D-D31014DBBE87}", 0, 1, 0) kompas_object = api5.KompasObject(Dispatch("Kompas.Application.5")._oleobj_.QueryInterface(api5.KompasObject.CLSID, pythoncom.IID_IDispatch)) kompas6_constants = gencache.EnsureModule("{75C9F5D0-B5B8-4526-8681-9903C567D2ED}", 0, 1, 0).constants # Подключим описание интерфейсов API7 api7 = gencache.EnsureModule("{69AC2981-37C0-4379-84FD-5DD2F3C0A520}", 0, 1, 0) application = api7.IApplication(Dispatch("Kompas.Application.7")._oleobj_.QueryInterface(api7.IApplication.CLSID, pythoncom.IID_IDispatch)) application.Visible = True api5_document = kompas_object.ActiveDocument2D() iDocument = application.ActiveDocument # iKompasDocument1 = kompas_api7_module.IKompasDocument1(iDocument) doc2D1 = api7.IKompasDocument2D1(iDocument) # Управление слоями # s = api5_document.ksLayer(3) # api5_document.ksLayer(0) # api5_document.ksDeleteObj(s) lmin = 300
# Restart GDS, and you should see some output generated. # from win32com.server.exception import COMException from win32com.client import gencache import winerror import pythoncom import sys # Specifics for GDS GDS_ICON = "no icon" GDS_FEXT = "hmf, herchu" # Support for COM objects we use. # Use these commands in Python code to auto generate .py support gds = gencache.EnsureModule('{3D056FE7-EA8E-481A-B18F-0B02EBF6B3C1}', 0, 1, 0) # The following class IS our COM Server. # Please DO NO reuse the same GUID. TITLE = "PySimpleAnytext" MYGUID = '{01C58A3B-18DB-4A5E-8F3E-3154C48DF4DE}' DESCRIPTION = "A very simple text indexer in Python." class GdsPlugin: _public_methods_ = ['HandleFile'] _reg_clsid_ = MYGUID _reg_desc_ = DESCRIPTION _reg_progid_ = TITLE _reg_policy_spec_ = "EventHandlerPolicy"