List all methods in COMobject List all methods in COMobject python python

List all methods in COMobject


For those who find the accepted answer not working (look here for the reasons) - there's still a way to get objects having a _prop_map_get_ attribute (a dict that holds object's fields as keys). You just have to create the main app object with win32com.client.gencache.EnsureDispatch().

Here's a convenience function I wrote that lists fields and methods of a passed COM object created that way:

from inspect import getmembersdef 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")

For an Excel object created with win32com.client.gencache.EnsureDispatch("Excel.Application") its output would be:

Members of 'Excel.Application' (Microsoft Excel):    Fields:        ActiveCell        ActiveChart        ActiveDialog        ActiveEncryptionSession        ...        Workbooks        WorksheetFunction        Worksheets        _Default    Methods:        ActivateMicrosoftApp        AddChartAutoFormat        AddCustomList        Calculate        ...        Union        Volatile        Wait


Just found how to get most of the methods:

Here's how:

import win32com.clientimport pythoncomProgID = "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

Here's a exemple output with ProgID = "Foobar2000.Application.0.7"

Output:

Playlists    Add    GetSortedTracks    GetTracks    Item    Load    Move    Remove    SaveName    foobar2000 v1.1.13ApplicationPath    C:\Program Files (x86)\foobar2000\foobar2000.exeMediaLibrary    GetSortedTracks    GetTracks    RescanMinimized    TruePlayback    FormatTitle    FormatTitleEx    Next    Pause    Previous    Random    Seek    SeekRelative    Start    StopProfilePath    file://C:\Users\user\AppData\Roaming\foobar2000


To list the attributes of an object you can use the dir() function. This is a built in function of python and does not need to be imported.Try something like:

print dir(object)

To see the attributes of the object.