Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - edit Powerpoint slide contents in a presentation that already exists

I'm using Python 2.7.2 and Office 2010 on Windows 7. I have a Powerpoint file that has links to other documents in a directory. I would like to use Python to loop through the shape captions, find matching names in the directory, and create a hyperlink in the Powerpoint file. As long as I can read those shape captions I can do the rest.

I managed to do this and at least open the Powerpoint file:

import win32com.client

Presentation = Application.Presentations.Open("c:\\path\\to\\stnd4.pptx")

I have found a number of ways to add slides that don't already exist and add shapes or captions, but I can't seem to find any way to edit existing slides. This lets me add a slide:

Base = Presentation.Slides.Add(1, 12)

But anything that tries to open or edit an existing slide just fails:

Base = Presentation.Slides.Open(1)
Base = Presentation.Slides.Edit(1)

I also tried

help(Presentation)

but I just get generic win32com info, nothing on Powerpoint slides. Googling didn't turn up much, either. Any clues?

like image 645
Tensigh Avatar asked Oct 18 '25 19:10

Tensigh


1 Answers

There's no concept of "opening" or "editing" a slide in the PowerPoint object model. Instead, you get a reference to a slide (which I suspect your "Base = Presentation.Slides.Add(1, 12)" line accomplishes).

PowerPoint has a hierarchical object model: Presentation contains Slides, Slides contain Shapes, Shapes have various properties that you can modify via their properties and methods.

I don't use Python or know how fully it can communicate with PPT, but:

Your BASE object (a slide, remember) probably has a Shapes collection. Iterate through the Shapes collection and for each shape try something like this:

If the shape's .HasTextFrame property is true then
   If the shape's .TextFrame.HasText property is true then
      The shape's .TextFrame.TextRange.Text property will return the text in the shape.
like image 58
Steve Rindsberg Avatar answered Oct 21 '25 09:10

Steve Rindsberg