这可能吗?
内容如下:
import win32com.client
ProgID = "someProgramID"
com_object = win32com.client.Dispatch(ProgID)
for methods in com_object:
print methods
我得到了 com_object.__dict__
,其中列出:
[_oleobj_, _lazydata_, _olerepr_, _unicode_to_string_, _enum_, _username_, _mapCachedItems_, _builtMethods_]
大部分都是空的,除了:
-
_oleobj_
(PyIDispatch) -
_lazydata_
(PyITypeInfo) -
_olerepr_
(LazyDispatchItem 实例) -
_username_
(<unknown>
)
但我不知道如何访问这些类型的任何内容。
最佳答案
对于那些找到 the accepted answer 的人不工作(查看 here 的原因) - 仍然有一种方法可以获取具有 _prop_map_get_
属性的对象(将对象的字段作为键保存的字典)。您只需使用 win32com.client.gencache.EnsureDispatch()
创建主应用程序对象。
这是我编写的一个便捷函数,它列出了以这种方式创建的传递的 COM 对象的字段和方法:
from inspect import getmembers
def print_members(obj, obj_name="placeholder_name"):
"""Print members of given COM object"""
try:
fields = list(obj._prop_map_get_.keys())
except AttributeError:
print("Object has no attribute '_prop_map_get_'")
print("Check if the initial COM object was created with"
"'win32com.client.gencache.EnsureDispatch()'")
raise
methods = [m[0] for m in getmembers(obj) if (not m[0].startswith("_")
and "clsid" not in m[0].lower())]
if len(fields) + len(methods) > 0:
print("Members of '{}' ({}):".format(obj_name, obj))
else:
raise ValueError("Object has no members to print")
print("\tFields:")
if fields:
for field in fields:
print(f"\t\t{field}")
else:
print("\t\tObject has no fields to print")
print("\tMethods:")
if methods:
for method in methods:
print(f"\t\t{method}")
else:
print("\t\tObject has no methods to print")
对于使用 win32com.client.gencache.EnsureDispatch("Excel.Application")
创建的 Excel 对象,其输出为:
Members of 'Excel.Application' (Microsoft Excel):
Fields:
ActiveCell
ActiveChart
ActiveDialog
ActiveEncryptionSession
...
Workbooks
WorksheetFunction
Worksheets
_Default
Methods:
ActivateMicrosoftApp
AddChartAutoFormat
AddCustomList
Calculate
...
Union
Volatile
Wait
关于python - 列出 COMobject 中的所有方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27370768/