3

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?

the
  • 21,007
  • 11
  • 68
  • 101
Tensigh
  • 1,030
  • 5
  • 22
  • 44

1 Answers1

1

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.
Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34
  • Thanks, that helps. Understanding the hierarchy of PP slides will help me poke around. I might be able to loop through them and update the links. – Tensigh Jul 02 '15 at 02:51
  • Each Slide also has a Hyperlinks collection that you can loop through. You'll be looking for the .Address and .SubAddress property of each hyperlink. I suspect that no matter what language you're using to automate PPT, you'll do well to fire up PPT, Alt+F11 to get into the VBA IDE, then do a bit of coding there, or at least press F2 to view the Object Browser. – Steve Rindsberg Jul 02 '15 at 14:05
  • Thanks, Steve. More and more it seems I have to learn VBA. I'd prefer to do this in Python since I already know it, but it seems like maybe VBA is the way to handle this. – Tensigh Jul 09 '15 at 05:23
  • 1
    It's not so much that you have to learn VBA, but if you already have PowerPoint, you've got VBA, and it gives you a fair amount of help in understanding the object model ... how PowerPoint "thinks". – Steve Rindsberg Jul 09 '15 at 14:37