通过 makepy 添加了 AutoCAD 的 typelib,现在 win32com 不适用于 AutoCAD
我已经运行 win32com
来访问 AutoCAD 相当长一段时间了,没有出现任何问题。我了解到应用 makepy
实用程序可以在交互式提示下创建更加用户友好的体验,因此我运行它并添加了“AutoCAD 2006 类型库”。现在,我以前访问的一些常用属性不再可用。考虑一下代码:
acad = win32com.client("AutoCAD.Application")
doc = acad.Documents.Open('mydoc.dwg')
ms = doc.ModelSpace
count = ms.Count #counts all entities in dwg
for i in range(count):
item = ms.Item(i)
if 'block' in item.ObjectName.lower():
print item.Name
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 462, in
__getattr__ raise AttributeError("'%s' object has no attribute '%s'"
(repr(self),attr))
AttributeError: '<win32com.gen_py.AutoCAD 2006 Type Library.IAcadEntity instance
at 0x34894552>' object has no attribute 'Name'
在我运行 makepy
之前,Name
一直是块的可访问属性。我尝试重新安装 Windows 版 Python,看看是否会重置它,但没有成功。
makepy 是否会在实现时创建另一个我需要删除的文件?
我的许多脚本都依赖于 Name
属性。如果您能提供任何建议或帮助,我们将不胜感激。
I have been running win32com
to access AutoCAD for quite some time without issue. I learned that applying the makepy
utility could create a more user friendly experience at the interactive prompt, so I ran it and added the "AutoCAD 2006 Type Library". Now, some common attributes that I used to access are no longer available. Consider the code:
acad = win32com.client("AutoCAD.Application")
doc = acad.Documents.Open('mydoc.dwg')
ms = doc.ModelSpace
count = ms.Count #counts all entities in dwg
for i in range(count):
item = ms.Item(i)
if 'block' in item.ObjectName.lower():
print item.Name
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 462, in
__getattr__ raise AttributeError("'%s' object has no attribute '%s'"
(repr(self),attr))
AttributeError: '<win32com.gen_py.AutoCAD 2006 Type Library.IAcadEntity instance
at 0x34894552>' object has no attribute 'Name'
Name
has always been an accessible attribute of a block until I ran makepy
. I've tried reinstalling Python for windows to see if that would reset it, but it didn't work.
Does makepy create another file upon implementation that I need to remove?
So many of my scripts depend upon the Name
attribute. Any suggestions or help you could offer would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此属性错误的主要原因是您的 COM 服务器已从后期绑定(动态)转变为早期绑定(静态)。
有两种方法可以解决此问题:
使用动态模块强制代码以面向后期绑定的方式工作。使用示例:
win32com.client.dynamic.Dispatch()
而不是win32com.client.Dispatch()
使用驼峰式敏感关键字进行早期绑定导向方式。使用示例:
excel.Visible()
而不是excel.VISIBLE()
或excel.visible()
因此,请尝试使用
dynamic。调度
或区分大小写的变量名称。The main reason for this attribute error is because your COM-server has shifted from late-binding (dynamic) to early binding (static).
There are two ways to fix this issue:
Use the dynamic module to force your code to work in a late-bound oriented way. Example use:
win32com.client.dynamic.Dispatch()
instead ofwin32com.client.Dispatch()
Use camelcase sensitive keywords for the early bound oriented way. Example use:
excel.Visible()
instead ofexcel.VISIBLE()
orexcel.visible()
So try using
dynamic.Dispatch
or case-sensitive variable names.当我运行
win32com
访问 DELMIA 时遇到类似的问题。我发现 delmia 有很多 com 内容作为.tlb
文件。当我输入:Python 会自动在
win32com\gen_py
目录中生成一些内容,与 Word 或 Excel 的示例相同。但是,不同之处在于生成的内容仅来自 delmia 的一个.tlb
文件。如果我访问`.tlb中的某个变量,那就没问题:如果我访问其他
.tlb
中的某个变量,我会得到类似的错误:Python说:
所以我删除了
win32com\gen_py
目录中的所有文件,但保留gen_py
目录,现在就可以了。I encounter a similar problem when I run
win32com
to access DELMIA. I find out that delmia has lots of com stuff as.tlb
files. When I type:Python will automatically generate some stuff in
win32com\gen_py
directory, same as example of Word or Excel. But, the difference is that the generated stuff is from only one.tlb
file of delmia. If I access some variable in the `.tlb, it's ok:If I access some variable in other
.tlb
, I get a similar error:Python says that:
So I delete all files in the
win32com\gen_py
directory but keepgen_py
directory, and it is ok now.