Search code examples
pythonpowerpoint

Changing font for part of text in python-pptx


I'm using the python-pptx module to create presentations. How can I change the font properties only for a part of the text?

I currently change the font like this:

# first create text for shape
ft = pres.slides[0].shapes[0].text_frame
ft.clear()
p = ft.paragraphs[0]
run = p.add_run()
run.text = "text"

# change font
from pptx.dml.color import RGBColor
from pptx.util import Pt

font = run.font
font.name = 'Lato'
font.size = Pt(32)
font.color.rgb = RGBColor(255, 255, 255)

Thanks!


Solution

  • In PowerPoint, font properties are applied to a run. In a way, it is what defines a run; a "run" of text sharing the same font treatment, including typeface, size, color, bold/italic, etc.

    So to make two bits of text look differently, you need to make them separate runs.