首页 > 解决方案 > 在 python 中使用 win32com 加载 COM 对象时出现问题

问题描述

我正在尝试通过 COM 对象与应用程序(数码显微照片)进行通信。这在 LabView 中有效,我现在正尝试在 python 中使用 win32com 做同样的事情。我收到了一系列错误,具体取决于我在 win32com 中的实现方式,这些错误并没有什么意义。

如果我按照快速入门指南尝试使用win32com.client.Dispatch(),我得到的对象除了来自 win32com 的默认值之外没有任何方法或类,尽管我知道 makepy 生成的模块有方法。

import win32com.client
sendDMscript = win32com.client.Dispatch('COMExamplePlugIn.COMExampleInterface')
dir(sendDMscript)

只退货

['_ApplyTypes_',
 '_FlagAsMethod',
 '_LazyAddAttr_',
 '_NewEnum',
 '_Release_',
 '__AttrToID__',
 '__LazyMap__',
 '__bool__',
 '__call__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattr__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__int__',
 '__le__',
 '__len__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_builtMethods_',
 '_enum_',
 '_find_dispatch_type_',
 '_get_good_object_',
 '_get_good_single_object_',
 '_lazydata_',
 '_make_method_',
 '_mapCachedItems_',
 '_oleobj_',
 '_olerepr_',
 '_print_details_',
 '_proc_',
 '_unicode_to_string_',
 '_username_',
 '_wrap_dispatch_']

如果我改为使用 gencache,则我想要的类存在,但是当我尝试创建它的实例时出现错误。

import win32com.client
sendDMscript_method = win32com.client.gencache.GetModuleForProgID('COMExamplePlugIn.COMExampleInterface').ICOMExampleInterface
instance = sendDMscript_method()

这返回

---------------------------------------------------------------------------
com_error                                 Traceback (most recent call last)
<ipython-input-4-55d05602784d> in <module>
----> 1 instance = sendDMscript()

~\Anaconda3\lib\site-packages\win32com\client\__init__.py in __init__(self, oobj)
    429         def __init__(self, oobj=None):
    430                 if oobj is None:
--> 431                         oobj = pythoncom.new(self.CLSID)
    432                 elif isinstance(oobj, DispatchBaseClass):
    433                         try:

com_error: (-2147221164, 'Class not registered', None, None)

我试图通过将正确的 oobj 作为参数传递给 来绕过这个错误sendDMscript_method(),这允许我创建类的实例,但当我尝试运行类中的一种方法时最终也会失败:

import win32com.client
from pywintypes import IID
sendDMscript_method = win32com.client.gencache.GetModuleForProgID('COMExamplePlugIn.COMExampleInterface').ICOMExampleInterface
CLSID = IID('{8C0B6A41-4ED2-4B44-B7B1-6492695B9C3D}')
instance = sendDMscript_method(CLSID)
instance.ExecuteScript(script='result("I did it")')

这会导致错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-8fdc545bf83d> in <module>
----> 1 instance.ExecuteScript(script='result("I did it")')

C:\Users\VALUED~1\AppData\Local\Temp\gen_py\3.7\8C0B6A41-4ED2-4B44-B7B1-6492695B9C3Dx0x2x0.py in ExecuteScript(self, script)
     57         def ExecuteScript(self, script=defaultNamedNotOptArg):
     58                 'method ExecuteScript'
---> 59         return self._oleobj_.InvokeTypes(1, LCID, 1, (5, 0), ((8, 1),),script
     60             )
     61 

AttributeError: 'PyIID' object has no attribute 'InvokeTypes'

当我用谷歌搜索这些错误时,我看到人们在谈论 32 位到 64 位的问题。我在 Anaconda 中运行 32 位 Windows 7 和 32 位 python 3.7,使用通过 Anaconda 安装的 pywin32。我还使用从 python.org 安装的 32 位 python 3.7 和通过 pip 安装的 pywin32 测试了快速入门指南的方法,并得到了相同的第一个错误。我尝试与之通信的应用程序也是 32 位的。有人知道出了什么问题吗?

标签: pythonpywin32win32com

解决方案


推荐阅读