Python: Update or Extract Slide Titles in PowerPoint

2024-03-29 07:56:24 Written by  support iceblue
Rate this item
(0 votes)

Slide titles play a crucial role in PowerPoint presentations because they can assist the audience in quickly grasping the topics or key points of each slide. When working with PowerPoint documents, users often encounter the need to update or extract slide titles for various purposes. For example, they may need to modify titles to reflect new content or extract titles to perform tasks such as summarizing or analyzing presentation content. Knowing how to programmatically update or extract slide titles can greatly save time and effort, particularly when dealing with extensive presentations. In this article, we will demonstrate how to update and extract slide titles in PowerPoint PPTX or PPT documents in Python using Spire.Presentation for Python.

Install Spire.Presentation for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows

Update Slide Titles in PowerPoint in Python

The title of a slide can be updated using the ISlide.Title property. The detailed steps are as follows.

  • Create a Presentation instance.
  • Load a PowerPoint PPTX or PPT document using Presentation.LoadFromFile() method.
  • Get a specific slide of the document using Presentation.Slides[index] property.
  • Update the title of the slide using ISlide.Title property.
  • Save the result document using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object
ppt = Presentation()
# Load a PowerPoint document
ppt.LoadFromFile("Water Of Life.pptx")

# Get the second slide
slide = ppt.Slides[1]

# Update the title of the second slide
slide.Title = "Updated Title"

# Save the result document
ppt.SaveToFile("UpdateSlideTitle.pptx", FileFormat.Pptx2016)
ppt.Dispose()

Python: Update or Extract Slide Titles in PowerPoint

Extract All Slide Titles from PowerPoint in Python

To extract all slide titles from a PowerPoint document, you first need to iterate through all slides in the document and all shapes on each slide. Then identify shapes with placeholder types like Title, CenteredTitle, or Subtitle. After that retrieve the text content from the identified shapes using the IAutoShape.TextFrame.Text property. The detailed steps are as follows.

  • Create a Presentation instance.
  • Load a PowerPoint PPTX or PPT document using Presentation.LoadFromFile() method.
  • Create a list to store the extracted titles.
  • Iterate through all slides in the document.
  • For each slide, iterate through all shapes on it.
  • Identify shapes with placeholder types such as Title, CenteredTitle, or Subtitle.
  • Typecast the identified shapes to IAutoShape object.
  • Retrieve the text content of the identified shapes using the IAutoShape.TextFrame.Text property and append them to the list.
  • Save the content of the list to a text file.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Load a PowerPoint document
ppt = Presentation()
ppt.LoadFromFile("Water of Life.pptx")

# Create a list to store the extracted slide titles
titles = []

# Iterate through all slides in the document
for slide in ppt.Slides:
    # Iterate through all shapes on each slide
    for shape in slide.Shapes:
        # Find the shapes with placeholder types such as Title, CenteredTitle, or Subtitle
        if shape.Placeholder is not None and shape.Placeholder.Type in [PlaceholderType.Title, PlaceholderType.CenteredTitle, PlaceholderType.Subtitle]:
            # Typecast the shape to IautoShape object
            auto_shape = shape if isinstance(shape, IAutoShape) else None
            if auto_shape is not None:
                # Add the text of the shape to the titles list
                titles.append(auto_shape.TextFrame.Text)

# Save the extracted slide titles to a text file
with open("AllTitles.txt", "w") as file:
    file.write("Extracted titles:\n")
    file.write("\n".join(titles))

ppt.Dispose()

Python: Update or Extract Slide Titles in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:18