Skip to content

Instantly share code, notes, and snippets.

@nmiglio
Created July 18, 2019 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmiglio/65fc118f625b26bd935cbb5f3ec0e295 to your computer and use it in GitHub Desktop.
Save nmiglio/65fc118f625b26bd935cbb5f3ec0e295 to your computer and use it in GitHub Desktop.
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")
@nmiglio
Copy link
Author

nmiglio commented Jul 18, 2019

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment