使用comtypes创建COM对象

2024-09-28 18:50:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将一些旧的VBA代码移植到Python。在

在VBA中,我添加Geo3D.dll作为项目引用,然后使用以下内容:

Set P1 = New GEO3DLib.Point
P1.Set 1, 2, 3, 0.001

在Python中,我尝试了以下方法:

^{pr2}$

但我得到一个错误:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    pt = cc.CreateObject('Geo3d.Point', None, None, Geo3d.Point)
  File "C:\Anaconda3\lib\site-packages\comtypes\client\__init__.py", line 238, in CreateObject
    obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
  File "C:\Anaconda3\lib\site-packages\comtypes\__init__.py", line 1217, in CoCreateInstance
    iid = interface._iid_
AttributeError: type object 'Point' has no attribute '_iid_'

将最后一行替换为:

pt = Geo3d.Point
pt.Set(1., 2., 3., 0.001)

我得到这个错误:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    pt.Set(1., 2., 3., 0.001)
AttributeError: type object 'Point' has no attribute 'Set'

有什么想法吗?在


Tags: inptmost错误linevbainterfacefile
1条回答
网友
1楼 · 发布于 2024-09-28 18:50:58

确保python实现和thinkdesign库是针对同一平台类型编译的:64位或32位。在

我测试如下:

>>> p=Dispatch('Geo3d.Point')
>>> p
<win32com.gen_py.think3 thinkdesign Type Library.IPoint instance at 0x59554312>
>>> p.Set(0,0,0)
>>> p.X
0.0

或使用comtypes:

^{pr2}$

相关问题 更多 >