I'm trying to replace multiple substrings inside a text-frame on Python-PPTX (PowerPoint Presentation module). I'm using the 'replace' string method to do it, one-by-one but when I save the presentation I noticed that it did not replaced my substrings.
PS: I'm using '.paragraphs[0].runs[0]' to keep the original formatting.
The whole string is: "Realizado no dia XX, com duração de YY horas.\nSão Paulo, ZZ de mês de ano."
I'm trying to replace the substrings: 'XX', 'YY', 'ZZ', 'mês' and 'ano'
The code:
from pptx import Presentation
prs = Presentation(r'template.pptx')
for slide in prs.slides:
for shape in slide.shapes:
if shape.has_text_frame:
if (shape.text.find('XX')) != -1:
text_frame = shape.text_frame
cur_text = text_frame.paragraphs[0].runs[0].text
new_text = cur_text.replace('XX',day)
new_text = new_text.replace('YY', '08')
new_text = new_text.replace('ZZ', day)
new_text = new_text.replace('mês',mes[int(mon)])
new_text = new_text.replace('ano', '2021')
text_frame.paragraphs[0].runs[0].text = new_text
prs.save('test.pptx')
Thanks