5

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
a b
  • 67
  • 1
  • 4

2 Answers2

6

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.
Diggy.
  • 6,744
  • 3
  • 19
  • 38
  • 1
    That's for the first paragraph, @Diggy. But extending to iterate over all the paragraphs isn't tough. (Yeah, 99% of the time this will complete the task.) – Martin Packer Jan 22 '21 at 19:42
2

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)
frab
  • 1,162
  • 1
  • 4
  • 14