I want to share what is working for me. I'm using Python 3.12.1
from pptx import Presentation
import pathlib
import datetime
template=pathlib.Path(r'C:\Template.pptx')
outputfile=pathlib.Path(r'C:\Output.pptx')
params={#your_keys_&_values
}
def search_and_replace(input, output,**kwargs):
""""search and replace text in PowerPoint while preserving formatting"""
prs = Presentation(input)
for slide in prs.slides:
for shape in slide.shapes:
for key, value in kwargs.items():
if not shape.has_text_frame:
continue
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
if key in run.text:
run.text=run.text.replace(key,value)
if shape.has_table:
for row in shape.table.rows:
for cell in row.cells:
paras = cell.text_frame.paragraphs
for para in paras:
for run in para.runs:
if key in run.text:
new_text = run.text.replace(str(key), str(value))
fn = run.font.name
fz = run.font.size
run.text = new_text
run.font.name = fn
run.font.size = fz
prs.save(output)
search_and_replace(template,outputfile,**params)