How Could I replace Text in a PPP with Python pptx?

python-pptx examples
python-pptx add text box
python-pptx edit existing slide
python-pptx bullet points
python-pptx multiple slides
python-pptx add existing slide
ppt to text python
edit powerpoint with python

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?

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")

pptx.text.text — python-pptx 0.6.18 documentation, encoding: utf-8 """Text-related objects such as TextFrame and Paragraph. rId) @address.setter def address(self, url): # implements all three of add, change, by either a member of the enumeration :ref:`PpParagraphAlignment` or |None|. Text exists in a hierarchy of three levels: Shape.text_frame; TextFrame.paragraphs; _Paragraph.runs; All the text in a shape is contained in its text frame. A text frame has vertical alignment, margins, wrapping and auto-fit behavior, a rotation angle, some possible 3D visual features, and can be set to format its text into multiple columns.

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')

Working with text — python-pptx 0.6.18 documentation, A text frame has vertical alignment, margins, wrapping and auto-fit behavior, a rotation angle, Note that this will not change color when the theme is changed:. It could also be used for making bulk updates to a library of presentations or simply to automate the production of a slide or two that would be tedious to get right by hand. More information is available in the python-pptx documentation. Browse examples with screenshots to get a quick idea what you can do with python-pptx.

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

python-pptx — python-pptx 0.6.18 documentation, python-pptx is a Python library for creating and updating PowerPoint (.pptx) files. elements; Add slides; Populate text placeholders, for example to create a bullet slide Access and change core document properties such as title and subject. def fit_text (self, font_family = "Calibri", max_size = 18, bold = False, italic = False, font_file = None,): """Fit text-frame text entirely within bounds of its shape. Make the text in this text frame fit entirely within the bounds of its shape by setting word wrap on and applying the "best-fit" font size to all the text it contains. :attr:`TextFrame.auto_size` is set to :attr:`MSO_AUTO_SIZE

Getting Started — python-pptx 0.6.18 documentation, from pptx import Presentation prs = Presentation() title_slide_layout Not all shapes can contain text, but those that do always have at least one paragraph,´┐Ż Since there's no API in python-pptx yet for changing the auto-fit setting, that's the only kind of text box you can have so far, auto-fit wise. When PowerPoint loads the file, it respects the width and height settings, until you make a change in the text.

python - 如何用python pptx替换PPP中的Text?, 我想用Python pptx替换Powerpoint中文本框中的文本。 我在网上找到的所有内容都对我不 How Could I replace Text in a PPP with Python pptx? This is why the text formatting disappears; because the runs that contained that formatting disappear. Although it would not work for all cases one might want, a useful behavior would be to replace the text in a paragraph, retaining the formatting present in the first run. This could be accomplished like this:

scanny/python-pptx, Although it would not work for all cases one might want, a useful behavior would be to replace the text in a paragraph, retaining the formatting´┐Ż A Shape object does not have a .font attribute (at least it didn't until you added one by assigning to that name :). Font is characteristic of a Run object; a run is a sequence of characters that all share the same character formatting, also loosely known as a font.