Using win32com To Work with Microsoft Office Applications

Requisites: Installing win32com

References:  Comprehensive Documentation on win32Com  This is a great reference but takes some reading

The following code will open a new PowerPoint document, add a diamond and save it.

import win32com.client
pptApp= win32com.client.gencache.EnsureDispatch (“Powerpoint.Application”)
presentation = pptApp.Presentations.Add()
slide = presentation.Slides.Add(1, 12)
myDiamond = slide.Shapes.AddShape(4, Top=100,Left=100, Width=20, Height=20)

So let’s break this down:

import win32com.client

Here we are importing the key module that will allow us to access Microsoft Office applications through the COM object. 

But what is a COM object?  It stands for Component Object Model, which, to me at least, means it gives , through an interface (COM object), to the Microsoft Office applications and their associated methods and attributes.

pptApp = win32com.client.gencache.EnsureDispatch (“Powerpoint.Application”)

In this line we are creating a reference (“pptApp“, you can call it whatever you want) to the powerpoint application

presentation = pptApp.Presentations.Add()

This line adds a presentation to the Presentations collection.

Slide = presentation.Slides.Add(1, 12)

This adds a slide to the Slides collection of the presentation.

The numbers  1 & 12 refer the slide index and layout respectively; in this example index=1 and layout = “blank”. 

The “12” may appear a bit cryptic, and it is.  12 is the number which if you look in MSDN enumeration represents a blank layout: ppslidelayout-enumeration-powerpoint

It is possible to use numbers and “google” the MSDN enumeration however, using constants makes for much readable code.  See this blog for creating a list of constants How to Generate Python Modules of MS Office Constants:

E.G: Slide = presentation.Slides.Add(1, MSOconstants.MSPPT.ppLayoutBlank).  The constant is ppLayoutBlank (the MSOconstants.MSPPT is the folder and python module where I have the constants held)

myDiamond = slide.Shapes.AddShape(4, Top=100,Left=100, Width=20, Height=20)

This lines creates a diamond (enumeration is the number 4; MSOconstants.MSO.msoShapeDiamond), 100 pixles from the top, 100 pixels from the left and 20×20 pixels in size.


This line saves the presentation as “myPowerPoint” and the 1 is enumeration of “powerpoint” format.



There are a number of variations but this works (you can read more here pywin32-docs-win32com-QuickStartClientCom)

See Reference Section for source material I found useful.


2 thoughts on “Using win32com To Work with Microsoft Office Applications

  1. Pingback: Reading and Interpreting MSDN Object Model Reference | Project Managers Toolkit

  2. Pingback: Flatten Microsoft Project to Pandas DataFrame | Project Managers Toolkit

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s