从Word.Application对象获取Document对象时,其成员不会从gencache缓存中填充,而VBSEdit能够获取它们的成员 [英] When getting Document object from Word.Application object, its members are not filled from gencache cache while VBSEdit is able to get them
问题描述
https://mail.python.org/pipermail /python-win32/2004-October/002582.html 完美地总结了我的问题(只需进行一些修改即可针对我的情况进行更新)
https://mail.python.org/pipermail/python-win32/2004-October/002582.html summarizes my problem perfectly (with slight edits to update it for my case):
我想拥有一个包含所有方法的Word Document实例, 互动会话期间可见的属性.这是我的currnet 方法:
I would like to have a Word Document instance with all methods and properties visible during interactive session. This is my currnet apporach:
>>> from win32com.client import gencache, Dispatch
>>> gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 1, bForDemand=True)
<module 'win32com.gen_py.00020905-0000-0000-C000-000000000046x0x8x1' from
'C:\DOCUME~1\osuchw\LOCALS~1\Temp\gen_py\2.3\00020905-0000-0000-C000-000000000046x0x8x1\__init__.py'>
>>> word = Dispatch('Word.Application')
>>> word
<win32com.gen_py.Microsoft Word 12.0 Object Library._Application instance at 0x18074720>
>>> doc = word.Documents.Add()
>>> doc
<win32com.gen_py.None.Document>
如您所见,我正在获取一个不知道其动态对象的对象 方法/属性.我想得到的是这样的:
As you can see I am getting a dynamic object not knowing its methods/properties. What I would like to get is something like:
>>> doc = Dispatch('Word.Document')
>>> doc
<win32com.gen_py.Microsoft Word 12.0 Object Library._Document instance at 0x37038040>
我想我还需要从另一个类型库(例如,有"Office 12.0 Object Library")生成包装器,但是没有关于它的提示.同样,在HKEY_CLASSES_ROOT\CLSID\{00020906-0000-0000-C000-000000000046}
上也没有TypeLib
子项,这是doc.CLSID
产生的.
I guess I need to also generate a wrapper from another type library (e.g. there is "Office 12.0 Object Library") but there's no hint on which it is. Likewise, there's no TypeLib
subkey at HKEY_CLASSES_ROOT\CLSID\{00020906-0000-0000-C000-000000000046}
which is what doc.CLSID
produces.
该线程中的唯一答案是直接向RTFM咨询有关Word编程的通用书籍,没有任何提示(我非常怀疑它甚至提到了类型库).
The only answer in that thread directs to RTFM a general book on Word programming w/o any hints whatsoever (I seriously doubt it even mentions typelibs).
我最终使用以下代码为计算机上的所有类型库生成了包装器:
I ended up generating wrappers for all the type libraries at the machine with the following code:
import win32com.client.selecttlb
ii=win32com.client.selecttlb.EnumTlbs()
ii=[i.desc for i in ii]
# one typelib - "Visual Basic runtime objects and procedures [6.0]" - produced an error
for i in ii: subprocess.call(['python','-m','win32com.client.makepy',i])
但是即使在这种情况下,它仍然是win32com.gen_py.None.Document
.
but even in this case, it's still win32com.gen_py.None.Document
.
最奇怪的是,对象的类 被识别并存在于缓存中:
The strangest thing is that the object's class is recognized and present in the cache:
>>> ad=win32com.client.gencache.EnsureDispatch("Word.Document")
>>> d = w.ActiveDocument
>>> d.__class__
<class win32com.gen_py.00020905-0000-0000-C000-000000000046x0x8x4.Document at 0x02A101B8>
>>> ad.__class__
<class win32com.gen_py.00020905-0000-0000-C000-000000000046x0x8x4._Document at 0x02A07B20>
>>> len(dir(ad.__class__))
141
>>> len(dir(d.__class__))
11
与此同时,VBA编辑器和 VBSEdit 列出了w.ActiveDocument
的成员.他们怎么做到的?为什么pythoncom
不能做到同样的事情?
At the same time, VBA editor and VBSEdit list w.ActiveDocument
's members just fine. How do they do that and why can't pythoncom
do the same?
推荐答案
d._dispobj_
具有所有成员,因此可以代替d
使用.
d._dispobj_
has all the members, so it can be used instead of d
.
眼前的问题解决了.
但是我还是不知道发生了什么事.
Yet I've still no idea what is going on.
这篇关于从Word.Application对象获取Document对象时,其成员不会从gencache缓存中填充,而VBSEdit能够获取它们的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!