Controlling PowerPoint w/ Python via COM32

Chase Kidder
3 min readOct 22, 2019

--

Purpose

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.

Setup

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/

Initializing an Application

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.

Controlling an Application

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.

I suggest using from constants_file import * to import them as it doesn't use the imported file namespace and is more VBA-esque.

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

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. You can request that the object close from the application level by using app.Quit().

--

--