How To Keep Original Text Formatting Of Text With Python Powerpoint?
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
Solution 1:
Text frame consists of paragraphs and paragraphs consists of runs. So you need to set text in run.
Probably you have only one run and your code can be changed like that:
from pptx importPresentationprs= 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.
Post a Comment for "How To Keep Original Text Formatting Of Text With Python Powerpoint?"