6

I am trying to get the title of each slide of a powerpoint file using Python. I am using Presentation package in Python but I couldn't find anything that specifies the titles. I have this code that return the content of the powerpoint file. but I need to specify the titles.

from pptx import Presentation

prs = Presentation("pp.pptx")

# text_runs will be populated with a list of strings,
# one for each text run in presentation
text_runs = []

for slide in prs.slides:
    for shape in slide.shapes:
        if not shape.has_text_frame:
            continue
        for paragraph in shape.text_frame.paragraphs:
            for run in paragraph.runs:
                text_runs.append(run.text)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Minerva
  • 63
  • 2
  • 9
  • The documentation suggests that you can access the shapes collection for a slide and determine the type of each shape. If it's a Type 14, it'd be a centered title placeholder. – Steve Rindsberg Nov 26 '16 at 17:34
  • 1
    The documentation mentions it specifically [here](http://python-pptx.readthedocs.io/en/latest/user/placeholders-using.html#setting-the-slide-title). It's a little bit tricky because the slide title shape is a *placeholder*, so the mention of it is after placeholders have been introduced. It's also the first entry returned from search on "slide title". – scanny Nov 26 '16 at 20:09

2 Answers2

10

This is my Solution:

from pptx import Presentation

filename = path_of_pptx

prs = Presentation(filename)

for slide in prs.slides:
    title = slide.shapes.title.text
    print(title)

Input:

enter image description here

Output:

Hello, World!
Hello, World2!
Hello, World3!
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

To build on @eyllanesc's answer, as @scanny points out, slide.shapes.title is a placeholder.

This means you can access the title text like:

from pptx import Presentation

prs = Presentation(ppt_filename)

slide = prs.slides[0]
slide.shapes.title.text = 'New Title'
print('New Title is:')
print(slide.shapes.title.text)

And also change any other of the title placeholder properties such as:

slide.shapes.title.top = 100
slide.shapes.title.left = 100
slide.shapes.title.height = 200
Pablo Guerrero
  • 936
  • 1
  • 12
  • 22