简体   繁体   English

如何在 python-pptx 中更改幻灯片标题的字体?

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

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

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)

This might be a bit hacky, but it works.这可能有点hacky,但它有效。

As per the docs , you can access the text_frame of the title placeholder shape .根据文档,您可以访问标题占位符形状text_frame
Thanks to that, you can fetch Paragraph objects within this frame with the paragraphs attribute.多亏了这一点,您可以使用paragraphs属性在此框架内获取Paragraph对象。 In the elements section here, you can see that the title placeholder shape is in the first index (if it exists).在此处的元素部分中,您可以看到title占位符形状位于第一个索引中(如果存在)。

Then we can now get the Font being used in the paragraph, and change different attributes of it, as follows:然后我们现在可以获取段落中使用的Font ,并更改它的不同属性,如下所示:

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. text.Font - 您可以编辑的更多字体属性。
  • util.Pt - Setting the font size. util.Pt - 设置字体大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM