微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我想更改幻灯片标题的字体大小和字体样式。我也想在标题下划线。如何做到这一点?

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

解决方法

用于应用字体样式:

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

改变字体大小:

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

这可能有点笨拙,但确实有效。

根据docs,您可以访问text_frametitle placeholder shape
因此,您可以使用 Paragraph 属性在此框架内获取 paragraphs 对象。在此处的 elements 部分,您可以看到 title 占位符形状位于第一个索引中(如果存在)。

那么我们现在可以得到段落中使用的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

额外参考:

  • text.Font - 您可以编辑更多字体属性。
  • util.Pt - 设置字体大小。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。