Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 10, 2022 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/2d16793c6097e415c7603742f163acae to your computer and use it in GitHub Desktop.
Save aspose-com-gists/2d16793c6097e415c7603742f163acae to your computer and use it in GitHub Desktop.
Find and Replace Text in PowerPoint PPT in Python
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