Python: Replace Text in PowerPoint Presentations

When editing or updating a large presentation, manually locating and modifying specific text elements can be a tedious and time-consuming process. By using the replace feature in PowerPoint, you can quickly and accurately make updates throughout the presentation, ensuring that the information remains accurate and consistent. In this article, we will demonstrate how to replace text in PowerPoint presentations 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

Replace the First Occurrence of a Specific Text in PowerPoint in Python

To replace the first occurrence of a specific text in a PowerPoint document, you can loop through all slides in the document, and then call the ISlide.ReplaceFirstText() method. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Loop through all slides in the PowerPoint document.
  • Replace the first occurrence of a specific text with new text using ISlide.ReplaceFirstText() method.
  • Save the result document using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create an object of the Presentation class
ppt = Presentation()
# Load a PowerPoint document
ppt.LoadFromFile("Sample.pptx")

# Loop through all slides in the document
for slide in ppt.Slides:
    # Replace the first occurrence of "Spire.Presentation for Python" with "E-iceblue Product"
    slide.ReplaceFirstText("Spire.Presentation for Python", "E-iceblue Product", False)
    break

# Save the result document
ppt.SaveToFile("ReplaceFirstTextOccurrence.pptx", FileFormat.Pptx2013)
ppt.Dispose()

Python: Replace Text in PowerPoint Presentations

Replace All Occurrences of a Specific Text in PowerPoint in Python

To replace all occurrences of a specific text in a PowerPoint document, you can loop through all slides in the document, and then use the ISlide.ReplaceAllText() method. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Loop through all slides in the PowerPoint document.
  • Replace all occurrences of a specific text with new text using ISlide.ReplaceAllText() method.
  • Save the result document using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create an object of the Presentation class
ppt = Presentation()
# Load a PowerPoint document
ppt.LoadFromFile("Sample.pptx")

# Loop through all slides in the document
for slide in ppt.Slides:
    # Replace all occurrences of "Spire.Presentation for Python" with "E-iceblue Product"
    slide.ReplaceAllText("Spire.Presentation for Python", "E-iceblue Product", False)

# Save the result document
ppt.SaveToFile("ReplaceAllTextOccurrences.pptx", FileFormat.Pptx2013)
ppt.Dispose()

Python: Replace Text in PowerPoint Presentations

Replace Text Using a Regular Expression in PowerPoint in Python

Spire.Presentation for Python provides the IShape.ReplaceTextWithRegex() method to replace text matching a regular expression pattern. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Loop through all slides in the PowerPoint document.
  • Loop through all shapes on each slide.
  • Replace text matching a regular expression pattern using IShape.ReplaceTextWithRegex() method.
  • Save the result document using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create an object of the Presentation class
ppt = Presentation()
# Load a PowerPoint document
ppt.LoadFromFile("Sample1.pptx")

# Loop through all slides in the document
for slide in ppt.Slides:
    # Loop through all shapes on each slide
    for shape in slide.Shapes:
        # Replace text starting with # on the slide to "Monitor"
        shape.ReplaceTextWithRegex(Regex("#\w+"), "Monitor")

# Save the result document
ppt.SaveToFile("ReplaceTextUsingRegex.pptx", FileFormat.Pptx2013)
ppt.Dispose()

Python: Replace Text in PowerPoint Presentations

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.