0

I want to replace the text in a textbox in Powerpoint with Python-pptx. Everything I found online didn't work for me and the documentation isn't that helpful for me.

So I have a Textbox with the Text:

  • $$Name 1$$
  • $$Name 2$$

and I want to change the $$Name1 $$ to Tom.

How can I achieve that?

vahdet
  • 6,357
  • 9
  • 51
  • 106
haelbito
  • 1
  • 1
  • 1

5 Answers5

0

A TextFrame object defined in python-pptx helps in manipulating contents of a textbox. You can do something like:

from python-pptx import Presentation
"""open file"""
prs = Presentaion('pptfile.pptx')
"""get to the required slide"""
slide = prs.slides[0]
"""Find required text box"""
for shape in slide.shapes:
    if not shape.has_text_frame:
        continue
    text_frame = shape.text_frame
    if "Name 1" == text_frame.text:
        text_frame.text = "Tom"
"""save the file"""
prs.save("pptfile.pptx")
windstorm
  • 375
  • 2
  • 10
0

Try this:

import pptx

input_pptx = "Input File Path"
prs = pptx.Presentation((input_pptx))
testString = "$$Name1 $$" 
replaceString = 'Tom'

title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)

for slide in prs.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            if(shape.text.find(testString))!=-1:
                shape.text = shape.text.replace(testString, replaceString)

        if not shape.has_table:
            continue    

prs.save('C:/test.pptx')
Anonymous
  • 659
  • 6
  • 16
0

Ok thank you. I just found out, that my PowerPoint example was totaly messed up. No everything works fine with a new PowerPoint blanked

haelbito
  • 1
  • 1
  • 1
0

In order to keep original formatting, we need to replace the text at the run level.

from pptx import Presentation    
ppt = Presentation(file_path_of_pptx)    
search_str = '$$Name1$$'
replace_str = 'Tom'

for slide in ppt.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            for paragraph in shape.text_frame.paragraphs:
                for run in paragraph.runs:
                    print(run.text)
                    if(run.text.find(search_str))!=-1:
                        run.text = run.text.replace(search_str, replace_str)

ppt.save(file_path_of_new_pptx)
derek.z
  • 907
  • 11
  • 19
0

Since PowerPoint splits the text of a paragraph into seemingly random runs (and on top each run carries its own - possibly different - character formatting) you can not just look for the text in every run, because the text may actually be distributed over a couple of runs and in each of those you'll only find part of the text you are looking for.

Doing it at the paragraph level is possible, but you'll lose all character formatting of that paragraph, which might screw up your presentation quite a bit.

Using the text on paragraph level, doing the replacement and assigning that result to the paragraph's first run while removing the other runs from the paragraph is better, but will change the character formatting of all runs to that of the first one, again screwing around in places, where it shouldn't.

Therefore I wrote a rather comprehensive script that can be installed with

python -m pip install python-pptx-text-replacer

and that creates a command python-pptx-text-replacer that you can use to do those replacements from the command line, or you can use the class TextReplacer in that package in your own Python scripts. It is able to change text in tables, charts and wherever else some text might appear, while preserving any character formatting specified for that text.

Read the README.md at https://github.com/fschaeck/python-pptx-text-replacer for more detailed information on usage. And open an issue there if you got any problems with the code!

Also see my answer at python-pptx - How to replace keyword across multiple runs? for an example of how the script deals with character formatting...

Frank
  • 884
  • 2
  • 10