As I've made slight progress with the problem at hand since i posted this question, I've found it necessary to split it in two parts to maintain clarity.
- How can I manipulate shape colors in PowerPoint using Python and win32com.client?
- How can I inspect com objects in Python using
dir()
?
1. Manipulate shape colors in PowerPoint using Python
There are some examples on how to edit PowerPoint slides using the pptx library here. However, I find it much easier to manipulate an active PowerPoint presentation using win32com.client as described here. Using an example from Microsoft Developer Network I've found that I can easily replicate parts of the functionality of this VBA snippet...
With ActivePresentation.Slides(1).Shapes(1)
With .TextFrame.TextRange.Font
.Size = 48
.Name = "Palatino"
.Bold = True
.Color.RGB = RGB(255, 127, 255)
End With
End With
...with this Python snippet:
import win32com.client
Application = win32com.client.Dispatch("PowerPoint.Application")
Presentation = Application.Activepresentation
slidenr = Presentation.Slides.Count
slide = Presentation.slides(slidenr)
shape1 = slide.Shapes.AddTextbox(Orientation=0x1,Left=100,Top=100,Width=100,Height=100)
shape1.TextFrame.TextRange.Text='Hello, world'
#Manipulate font size, name and boldness
shape1.TextFrame.TextRange.Font.Size=20
shape1.TextFrame.TextRange.Characters(1, 4).Font.Name = "Times New Roman"
shape1.TextFrame.TextRange.Font.Bold=True
Here I'm able to manipulate font size and name. I can also change the orientation of the textbox by changing
Orientation=0x1
to Orientation=0x5
in shape1 = slide.Shapes.AddTextbox(Orientation=0x1,Left=100,Top=100,Width=100,Height=100)
.
What seems impossible though, is editing the box or font color.
This does not work:
shape1.TextFrame.TextRange.Font.Color.RGB = RGB(255, 127, 255)
Error message:
I had high hopes to fix this by importing some RGB functionality following the information on pypi.python.org
But I'm having trouble here as well with pip install colour
:
By now I'm a bit lost on all accounts, so any hints to ANY way of manipulating colors would be great!
2. Inspect objects in Python using dir()
In my attempts to manage those pesky colors, I started inspecting the output from dir(shape1.TextFrame)
, dir(shape1.TextFrame.Textrange)
and so on.
To my disappointment, I could not find anything about colors, and not even Font, although Font is clearly accessible for manipulation.
So my second question is this: Is this not the way to inspect and manipulate these shapes at all? And how could I find the right object (or method?) to manipulate shape1 further? I have had a look at the PowerPoint objectmodel, but with little success.
Thank you for any suggestions!