python列出所有方法_列出COMobj中的所有方法

对于那些发现the accepted answer不起作用的人(我猜有些东西随着pywin32在Python3中的工作方式而改变了)-仍然有一种方法可以获得具有_prop_map_get_属性(将对象的字段作为键保存的dict)的对象。您只需使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值