Find & replace values in presenter notes using pptx-python

118 Views Asked by At

I have a dictionary of values that I'd like to use to find & replace in the presenter notes of a powerpoint presentation. My code works for replacing the values in the slides themselves, but I can't figure out the presenter notes. This is what I have so far - any help would be appreciated!

from pptx import Presentation
prs = Presentation('templates/template_python.pptx')

#dictionary of key-values to find & replace in presentation & presenter notes

replacements = {
    'client_name': 'example',
    'date_range': '2023-01-01 to 2023-01-30',
    'country_abb': country,
    }

slides = [slide for slide in prs.slides]
shapes = []
for slide in slides:
    for shape in slide.shapes:
        shapes.append(shape)

#find & replace text in presentation - **WORKING**
def replace_text(replacements, shapes):
    for shape in shapes:
        for match, replacement in replacements.items():
            if shape.has_text_frame:
                if (shape.text.find(match)) != -1:
                    text_frame = shape.text_frame
                    for paragraph in text_frame.paragraphs:
                        whole_text = "".join(run.text for run in paragraph.runs)
                        whole_text = whole_text.replace(str(match), str(replacement))
                        for idx, run in enumerate(paragraph.runs):
                            if idx != 0:
                                p = paragraph._p
                                p.remove(run._r)
                        if(not(not paragraph.runs)):
                            paragraph.runs[0].text = whole_text

#run function to find & replace text in slides
replace_text(replacements, shapes)

#find & replace text in presenter notes **NOT WORKING**
def replace_in_presenter_notes(presenter_notes, replacements):
    for key, value in replacements.items():
        presenter_notes = presenter_notes.replace(key, str(value))
    return presenter_notes

# Iterate through the slides and update presenter notes
for slide in prs.slides:
    for shape in slide.shapes:
        if hasattr(shape, 'notes_text_frame') and shape.notes_text_frame.text:
            shape.notes_text_frame.text = replace_in_presenter_notes(shape.notes_text_frame.text, replacements)
1

There are 1 best solutions below

0
On

Borrowing from the answer to this question, the presenter notes can be accessed with slide.notes_slide.notes_text_frame.text.

Working code example:

from pptx import Presentation
from pathlib import Path

pptx_file = Path('path\\to\\directory', 'test.pptx')
replacements = {
    'client_name': 'example',
    'date_range': '2023-01-01 to 2023-01-30',
    'country_abb': 'new_country',
    }

prs = Presentation(pptx_file)
for slide in prs.slides:
    note = slide.notes_slide.notes_text_frame.text
    for k, v in replacements.items():
        note = note.replace(k, v)
    slide.notes_slide.notes_text_frame.text = note
prs.save(pptx_file)

Link to Documentation on notes slide.