f.r*_*ues 11 python methods win32com
可能吗?
有些东西:
import win32com.client
ProgID = "someProgramID"
com_object = win32com.client.Dispatch(ProgID)
for methods in com_object:
print methods
Run Code Online (Sandbox Code Playgroud)
我得到了com_object.__dict__
,其中列出:
[_oleobj_, _lazydata_, _olerepr_, _unicode_to_string_, _enum_, _username_, _mapCachedItems_, _builtMethods_]
Run Code Online (Sandbox Code Playgroud)
大多数都是空的,除了:
_oleobj_
(PyIDispatch)_lazydata_
(PyITypeInfo)_olerepr_
(LazyDispatchItem实例)_username_
(<unknown>
)但我不知道如何访问这些类型的任何东西.
对于那些发现接受的答案不起作用的人(我猜想它pywin32
在Python3中的工作方式有所改变)-仍然有一种方法来获取具有_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")
Run Code Online (Sandbox Code Playgroud)
对于使用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
Run Code Online (Sandbox Code Playgroud)
刚刚找到如何获得大多数方法:
这是如何做:
import win32com.client
import pythoncom
ProgID = "someProgramID"
com_object = win32com.client.Dispatch(ProgID)
for key in dir(com_object):
method = getattr(com_object,key)
if str(type(method)) == "<type 'instance'>":
print key
for sub_method in dir(method):
if not sub_method.startswith("_") and not "clsid" in sub_method.lower():
print "\t"+sub_method
else:
print "\t",method
Run Code Online (Sandbox Code Playgroud)
这是一个示例输出 ProgID = "Foobar2000.Application.0.7"
输出:
Playlists
Add
GetSortedTracks
GetTracks
Item
Load
Move
Remove
Save
Name
foobar2000 v1.1.13
ApplicationPath
C:\Program Files (x86)\foobar2000\foobar2000.exe
MediaLibrary
GetSortedTracks
GetTracks
Rescan
Minimized
True
Playback
FormatTitle
FormatTitleEx
Next
Pause
Previous
Random
Seek
SeekRelative
Start
Stop
ProfilePath
file://C:\Users\user\AppData\Roaming\foobar2000
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8820 次 |
最近记录: |