pythonpowerpointcomtypes

Using file format constants when saving PowerPoint presentation with comtypes


How do I access the constants available as file formats when saving a Powerpoint presentation through comtypes?

In the following example 32 is used as the format but I would like to use the constants listed here) or at least find a documented list with value for each constant.

For Word there is this list that also contains the value for each constant.

import comtypes.client

powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
pres = powerpoint.Presentations.Open(input_path)
pres.SaveAs(output_path, 32)

Solution

  • You can access all enum names associated with the COM object you loaded via the comtypes.client.Constants() class; pass it the PowerPoint.Application COM object you created:

    from comtypes.client import Constants, CreateObject
    
    powerpoint = CreateObject("Powerpoint.Application")
    pp_constants = Constants(powerpoint)
    
    pres = powerpoint.Presentations.Open(input_path)
    pres.SaveAs(output_path, pp_constants.ppSaveAsPDF)
    

    The Constants instance loads the underlying Type Library and dynamically translates attribute lookups to typelib access. It is not included in the comtypes documentation for some obscure reason, even though it was added nearly 10 years ago now.

    Another option is to access the attributes on a generated module in the generated type library, as shown in the Properties with arguments (named properties) section. This would give you access to any of the constants associated with the Powerpoint IDL, including auto completion support IDEs (once generated by accessing the PowerPoint.Application object the first time around).

    The module is generated automatically when you use CreateObject() if type information is exposed on the object that's being created; this is definitely the case for 'Powerpoint.Application' as you don't set an interface explicitly. Automatic interface selection only works if there is type information available.

    Enumeration names are added to the generated module at the top level, so directly use those:

    import comtypes.client
    
    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    
    # only import the generated namespace after the com object has been created
    # at least once. The generated module is cached for future runs.
    from comtypes.gen import PowerPoint
    
    pres = powerpoint.Presentations.Open(input_path)
    pres.SaveAs(output_path, PowerPoint.ppSaveAsPDF)
    

    The short name of the type library can be found in a VBA Object Browser; the screenshot in Steve Rindsberg's answer shows that for the PpSaveAsFileType enum that's PowerPoint. I believe the same name is also used in the documentation for the ppSaveAsFileType enum; note the (PowerPoint) addition to the documentation title.

    You can also use the GUID of the type library, plus version number, but that doesn't quite roll of the keyboard if you have to type that by hand.

    You can use from comtypes.gen import PowerPoint; help(PowerPoint) to see what names have been defined if you need a reminder, or just reference the Microsoft documentation.

    Either method avoids having to use magic numbers; the type library definition itself gives you the symbolic names.

    If you find any code examples using win32com instead, then any use of win32com.client.constants attributes translates directly to comtypes.client.Constant(...) or comtypes.gen.<module> attributes.


    I don't have access to a Windows setup to actually test any of this, I'm deducing information from reading documentation and the source code of comtypes.