This post explains how to use Python and COM to automate PowerPoint. COM provides a much deeper level of control than any of the Python modules that exist at this time. It exposes the entire VBA API to Python, allowing us to use Python’s superior pretty much everything (my opinion) to take advantage of VBA’s deep integration with MS Office.
This process can be used with any MS Office software that supports VBA. So, you could automate Word, Excel, Access, … etc. in the same ways.
Before working with COM, we need to find a way to interface over COM. To do this we can use the Python package pywin32 to provide us with useful functions and an easy way to talk to a COM32 program. In this guide I am using Python 3.
https://pypi.org/project/pywin32/
In order to control an application, we first need to create a connection to the application itself. This command opens a COM32 connection to PowerPoint that we can send commands over.
app = win32com.client.Dispatch("PowerPoint.Application")
The app
object is now our entry point to the PowerPoint Object controls. We can now create an object that contains an open .pptx by using a bit of VBA-esque code.
class ppt:
def __init__(self):
self.objCOM = app.Presentations.Open(FileName="path_to_file", WithWindow=1)
We now have an object, ppt
, that contains another object, objCOM
, that is a direct handle on the ‘presentation’ object level in VBA. We can now use objCOM
as a direct replacement for a VBA object such as activePresentation
. So,
activePresentation.SlideShowWindow.View.Next()
(VBA) becomes self.objCOM.SlideShowWindow.View.Next()
(python)
if used within the ppt class or inst_name.objCOM.SlideShowWindow.View.Next()
(python) if used by an external method.
Using the ideas from above, we can set, read, or use almost any method or variable made available to us through the VBA object library. We can utilize the Microsoft Office Object Reference Library to discover usable properties and functions. This API is available for all MS Office apps. Here are a few examples of available objects using the PowerPoint object from earlier.
Creating a New SlideShow (creating an object)
self.objCom.Presentations.Add
Advancing a Slide (controlling an object)
self.objCOM.SlideShowWindow.View.Next()
This advances the SlideShow to the next unhidden slide.
Creating a Slide (creating a sub-object)
self.objCOM.Slides.Add Index:=ActivePresentation.Slides.Count + 1
The Index is where in the slide show you want to add the slide.
Setting The SlideShow to Advance Manually Only (setting a property)
self.objCOM.SlideShowSettings.AdvanceMode = ppAdvanceOnClick
'ppAdvanceOnClick' is only available if you import the Microsoft PowerPoint 12.0 Object Library constants in your code. Otherwise, you need to convert these into their hex value equivalents.
Reading Current Slide Index (reading a property)
self.objCOM.SlideShowWindow.View.Slide.SlideIndex
The SlideIndex is a unique slide identifier that can be used to refer to any slide in the SlideShow. Each slide has one (including hidden slides) and they are incremented in order of the slides in the SlideShow. eg. 1, 2, 3, ...
Closing an Application `app.Quit()
Closing the application is optional as the process will be closed automatically when all references to it have been deleted. However, it is good practice to close the reference at the end of your program or when you are finished with all PowerPoint objects.
Here are some of the Object Libraries that contain the PowerPoint COM constants. I prefer using from MSPPT import *
and from MSO import *
to import them as it doesn't use the file's namespace and is more VBA-esque.
If some constants are missing or if we want to generate a python importable Object Library out of an OLE TypeLibrary (DLL/OXC/TLB) file, we can use the makepy
script from the Pywin32 github repository to generate one. By calling the script on a file that contains COM type information, we can automatically generate a python compatible version that can give us the COM constants.
For example, We can create a file with the latest PowerPoint Types by running makepy.py
.
Usage:
makepy.py [-i] [-v|q] [-h] [-u] [-o output_file] [-d] [typelib, ...]
-i -- Show information for the specified typelib.
-v -- Verbose output.
-q -- Quiet output.
-h -- Do not generate hidden methods.
-u -- Python 1.5 and earlier: Do NOT convert all Unicode objects to
strings.
Python 1.6 and later: Convert all Unicode objects to strings.
-o -- Create output in a specified output file. If the path leading
to the file does not exist, any missing directories will be
created.
NOTE: -o cannot be used with -d. This will generate an error.
-d -- Generate the base code now and the class code on demand.
Recommended for large type libraries.
typelib -- A TLB, DLL, OCX or anything containing COM type information.
If a typelib is not specified, a window containing a textbox
will open from which you can select a registered type
library.
Listing All Registered Type Libraries
makepy.py -d
Generating Python Constants File
makepy.py -d "Microsoft Excel 15.0 Object Library"