Read the complete article on how to find and replace text in PowerPoint PPT in Python: https://blog.aspose.com/2022/01/10/find-and-replace-text-in-ppt-in-python/
Last active
February 10, 2022 13:57
-
-
Save aspose-com-gists/2d16793c6097e415c7603742f163acae to your computer and use it in GitHub Desktop.
Find and Replace Text in PowerPoint PPT in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.slides as slides | |
# load presentation | |
with slides.Presentation("presentation.pptx") as pres: | |
strToFind = "Master" | |
strToReplaceWith = "[replaced]" | |
# loop through each slide | |
for slide in pres.slides: | |
# get all text frames in the slide | |
tf = slides.util.SlideUtil.get_all_text_boxes(slide) | |
# loop through text frames | |
for i in range(len(tf)): | |
# loop through paragraphs in text frame | |
for para in tf[i].paragraphs: | |
# loop through text portions in paragraph | |
for port in para.portions: | |
# check if search string appears in text portion | |
if strToFind in port.text: | |
# replace exisitng text with the new text | |
str = port.text | |
idx = str.index(strToFind) | |
strStartText = str[0: idx] | |
strEndText = str[idx + len(strToFind): len(str)] | |
port.text = strStartText + strToReplaceWith + strEndText | |
# save the presentation | |
pres.save("find-and-replace-text.pptx", slides.export.SaveFormat.PPTX) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment