How to keep original text formatting of text with python powerpoint?
Asked Answered
S

3

4

I'd like to update the text within a textbox without changing the formatting. In other words, I'd like to keep the original formatting of the original text while changing that text

I can update the text with the following, but the formatting is changed completely in the process.

from pptx import Presentation
prs = Presentation("C:\\original_powerpoint.pptx")
sh = prs.slides[0].shapes[0]
sh.text_frame.paragraphs[0].text = 'MY NEW TEXT'
prs.save("C:\\new_powerpoint.pptx")

How can I update the text while maintaining the original formatting?

I've also tried the following:

from pptx import Presentation
prs = Presentation("C:\\original_powerpoint.pptx")
sh = prs.slides[0].shapes[0]
p = sh.text_frame.paragraphs[0]
original_font = p.font
p.text = 'NEW TEXT'
p.font = original_font

However I get the following error:

Traceback (most recent call last):
  File "C:\Codes\powerpoint_python_script.py", line 24, in <module>
    p.font = original_font
AttributeError: can't set attribute
Stalinist answered 21/7, 2017 at 21:35 Comment(2)
It isn't obvious which line of the code you posted gives the error. And give the full error message.Gula
Thanks for the suggestion. I've just posted up above.Stalinist
L
10

Text frame consists of paragraphs and paragraphs consists of runs. So you need to set text in run. enter image description here

Probably you have only one run and your code can be changed like that:

from pptx import Presentation
prs = Presentation("C:\\original_powerpoint.pptx")
sh = prs.slides[0].shapes[0]
sh.text_frame.paragraphs[0].runs[0].text = 'MY NEW TEXT'
prs.save("C:\\new_powerpoint.pptx")

Character formatting (font characteristics) are specified at the Run level. A Paragraph object contains one or more (usually more) runs. When assigning to Paragraph.text, all the runs in the paragraph are replaced with a single new run. This is why the text formatting disappears; because the runs that contained that formatting disappear.

Landbert answered 3/7, 2018 at 7:19 Comment(3)
If you use PPT's Replace method on the text run rather than just changing the text, the formatting is preserved. Whether or not that's accessible in Python is an exercise for the reader and other helpful Python-clueful souls. I fail the second of the two tests. ;-)Benilda
Useful answer. But other than formatting issues like bold, italics, could there be any other reason why a new run is created? How do we replace our search term when they are present across multiple runs? I am facing a similar problem but bit more conplicated to replace. Any help please #73219878Charterhouse
And can't we retain formatting at the paragraph level? Because if I am not able to replace words through text run, I may replace it using paragraph text (as substring replace) but is it possible to copy and reassign the same formatting back to the paragraoh text (after replacement)?Charterhouse
E
1

Use the petpptx package, this is an updated fork of python-pptx

python -m pip install petpptx

Saved previous font style

font_style= text_frame.paragraphs[0].runs[0].font
text_frame.clear()
run = text_frame.paragraphs[0].add_run()
run.font = font_style
run.text = "Hello World"

Copied font

font_style= text_frame.paragraphs[0].runs[0].font
text_frame.paragraphs[0].runs[1].font = deepcopy(font_style)

Elemi answered 6/6, 2023 at 11:19 Comment(0)
D
0

The font attribute can not be assigned directly in python-pptx. You can only assign attributes of the font attribute itself, like size, name, etc. That's why you get the AttributeError.

In general, if you want to replace text in a PowerPoint presentation while keeping the character formats, you need to replace the text on the run level because that is, where the character formats are ultimately stored (there is also a default font on paragraph level and another on text_frame level, and those are the ones used, if you assign the text at paragraph- or text_frame level).

The problem with replacing text in a PowerPoint presentation is, that any text can be distributed over any number of paragraph runs, thus looking for a specific text must be done on text_frame or paragraph level, but it must be replaced on run-level.

Using the package python-pptx-text-replacer installable through

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

will allow you to do that easily either on command line or using the class TextReplacer in that package in your own Python scripts.

Deuterogamy answered 13/8, 2022 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.