While processing digital documents, you often need to find and replace a specific text in the content. This could be required when a piece of information is to be censored before sharing the documents. To accomplish this programmatically, this article covers how to find and replace text in PowerPoint PPT or PPTX in Python.
Info: Aspose recently developed a free online Text to GIF service that allows you to animate texts or generate GIFs from simple texts.
Python Library to Find and Replace Text in PowerPoint PPT
Aspose.Slides for Python via .NET is an amazing library that simplifies the manipulation of PowerPoint presentations from within Python applications. Moreover, you can create rich presentations from scratch seamlessly. We will use this library to find and replace text inPPT/PPTX files. You can use the following pip command to install the library from PyPI.
> pip install aspose.slides
Find and Replace Text in PowerPoint PPT using Python
To replace a particular piece of text in the presentation, you will have to go through the content of all the slides, which is quite easy using Aspose.Slides. The following are the steps to find and replace text in a PPTX presentation in Python.
- First, load the PowerPoint PPT/PPTX using Presentation class.
- Then, loop through the slides using Presentation.slides collection.
- In each iteration, get text frames of the slide using SlideUtil.get_all_text_boxes(slide) method.
- Loop through text frames and in each iteration, perform the following operations:
- Loop through the paragraphs in the text frame.
- Access the text portions in each paragraph.
- Check if text portion contains the search string.
- If yes, find the position of the search string and replace the text.
- Save the updated PPT using Presentation.save(string, SaveFormat) method.
The following code sample shows how to find and replace text in a PowerPoint PPTX in Python.
The following screenshot shows the input PowerPoint presentation.
Below is the screenshot of the resultant presentation after replacing the text.
Get a Free License
You can use Aspose.Slides for Python via .NET without evaluation limitations by requesting a temporary license.
In this article, you have learned how to find and replace text in PowerPoint PPT in Python. You can install Aspose.Slides and automate finding and replacing text from within your Python applications. Besides, you can read the documentation to explore more about Aspose.Slides for Python. Also, you can post your queries to our forum.