How change the font of a title of a slide in python-pptx?

Viewed 1382

I want to change the font size and font style of the title of the slide. I also want to underline the title. How to do that?

from pptx import Presentation

prs = Presentation(ppt_filename)

slide = prs.slides[0]
slide.shapes.title.text = 'New Title'
slide.shapes.title.top = 100
slide.shapes.title.left = 100
slide.shapes.title.height = 200
2 Answers

This might be a bit hacky, but it works.

As per the docs, you can access the text_frame of the title placeholder shape.
Thanks to that, you can fetch Paragraph objects within this frame with the paragraphs attribute. In the elements section here, you can see that the title placeholder shape is in the first index (if it exists).

Then we can now get the Font being used in the paragraph, and change different attributes of it, as follows:

from pptx import Presentation
from pptx.util import Pt

prs = Presentation(ppt_filename)

slide = prs.slides[0]
slide.shapes.title.text = 'New Title'
slide.shapes.title.top = 100
slide.shapes.title.left = 100
slide.shapes.title.height = 200

title_para = slide.shapes.title.text_frame.paragraphs[0]

title_para.font.name = "Comic Sans MS"
title_para.font.size = Pt(72)
title_para.font.underline = True

Extra References:

  • text.Font - More font attributes you can edit.
  • util.Pt - Setting the font size.

For applying a font style:

slide.shapes.title.font.name = 'Calibri'

For changing the font size:

from pptx.util import Pt
slide.shapes.title.font.size = Pt(20)
Related