In this quick post I am using the function demonstrated in this post. I have wrapped it into a module and used it in a notebook to read a MS Project file, then output the results of the flattened MS Project to excel workbooks which I then formatted using a quick and dirty self made excel formatting module (code can be found here).
My initial months working with win32com saw me searching and reading MSDN Object Model References, reading other peoples win32com python code, and trawling VBA snippets whilst bashing away in a python IDE trying to get the function I was after. So here are my tips in reading the MSDN object references with the intent of replicating the functionality using win32com in Python.
The structure is roughly:
Let’s start simple and working from where this article left off but with constants.
Step 1: Import modules and constants
import win32com.client from MSOconstants.MSO import constants as MSO from MSOconstants.MSPPT import constants as MSPPT
Step 2: Create and set-up references to a powerpoint application, presentation, slide, add a diamond and save the presentation.
So far so good. I have a powerpoint slide with diamond thus:
Let’s say I want to add a bar to the slide that represents timeline between two dates such that I can place myDiamond on the timeline at a place that represents that point in time along the timeline; say 1/4 along the timeline.
I need to:
Determine Slide Dimension
Draw a box the width of the slide using the slide dimension
place myDiamond 1/4 away along the box
Step 3: Determine Slide Dimension
A slide is dimensioned in pixels. I want to get the dimensions of my slide. I google “msdn powerpoint slide width” and get this link as the first entry.
So we need an object that represents the presentation “Application.ActivePresentation” so that we can call the PageSetup objects SlideWidth property. In our case we have a reference to the presentation in the variable “presentation“. So we call the presentation.PageSetup.SlideWidth property
for me it is 960 pixels.
Note: we can’t use “with” statements in Python.
Step 4: Draw a box the width of the slide using the slide dimension
I google “msdn powerpoint add shape” and get this link first
The snippet below creates four python module for each of Microsoft Office, Excel, Word and Powerpoint in the “MSOconstants” directory of my Jupyter Lab notebook location (I’m using Jupyter Lab notebooks. You can reference any folder location of your choice).
Note: if you have Microsoft Project then you can create the equivalent by referencing “Microsoft Office 14.0 Object Library” (14.0 is the version I happen to have, you may have a different version). This is important as in a later blog I will outlining how to use win32com with MS Project.
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.