Created
July 18, 2019 12:02
-
-
Save nmiglio/65fc118f625b26bd935cbb5f3ec0e295 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from inspect import getmembers | |
def print_com_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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create the main COM object with
win32com.client.gencache.EnsureDispatch()
The function prints a list of fields and methods exposed by the object.
(courtesy of z33k in stackoverflow)