1. 利用ocx名稱
import win32com.client
w = win32com.client.Dispatch('Word.Application')
# 或者使用下面的方法,使用啓動獨立的進程:
# w = win32com.client.DispatchEx('Word.Application')
2. 利用classid
import win32com.client.gencache as win32
ocx_classid = '{3F166327-8030-4881-8BD2-EA25350E574A}'
ocx = win32.EnsureModule(ocx_classid, 0, 1, 0)
3. 實例
在wxpyton中引入ocx
用ocx函查看器查看我們要引用的ocx
classid取綠色框中的文本
ActiveXWrapper = MakeActiveXClass(ktocx.KTSEDocAxEx)
import wx
from wx.lib.activexwrapper import MakeActiveXClass
import win32com.client.gencache as win32
class MainFrame(wx.Frame):
def __init__(self,parent=None,title="test"):
super().__init__(parent,title=title,size=(900,800))
box = wx.Panel(self, -1, style=wx.FULL_REPAINT_ON_RESIZE)
self._ktocx(box)
self.Centre()
self.Show()
def _ktocx(self,box):
ocx_classid = '{5A79EFEB-0D35-4894-BC8C-CBA69803B5C3}'
ktocx = win32.EnsureModule(ocx_classid, 0, 1, 0)
if ktocx is None:
wx.MessageBox('未安裝凱特簽章驅動', '錯誤', wx.OK | wx.ICON_WARNING)
self.Close(True)
ActiveXWrapper = MakeActiveXClass(ktocx.KTSEDocAxEx)
self.ocx = ActiveXWrapper(box, -1, size=(945, 810))
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
app.MainLoop()
效果圖: