[英]How to change the font size of an python pptx object
I would like to change the font size of the title and of the body of my pptx-presentation.我想更改 pptx 演示文稿的标题和正文的字体大小。 I tried to set it via
title_shape.font = Pt(15)
and body_shape.font = Pt(10)
, which does not work.我试图通过
title_shape.font = Pt(15)
和body_shape.font = Pt(10)
来设置它,但这不起作用。
Here is my code:这是我的代码:
from pptx import Presentation, util, text
from pptx.util import Cm, Pt
import fnmatch
import os
import contentOf_pptx as contOfPres
# ..............
# Generate presentation
# ..............
prs = Presentation()
#blank_slide_layout = prs.slide_layouts[6] #blank layout, see slide layout in powerpoint
title_only = prs.slide_layouts[5] #title only, see slide layout in powerpoint
# ..............
# set layout
# ..............
bullet_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes
title_shape = shapes.title
title_shape.font = Pt(15)
body_shape = shapes.placeholders[1]
body_shape.font = Pt(10)
# ..............
# set relevant text objects
# ..............
title_shape.text = 'Test Title'
tf = body_shape.text_frame
tf.text = 'Test SubText'
# ----------------------------------
# Store pptx
# ----------------------------------
prs.save('C:\\tests\\test_pptx_python.pptx')
A Shape
object does not have a .font
attribute (at least it didn't until you added one by assigning to that name :) Shape
对象没有.font
属性(至少在您通过分配给该名称添加一个属性之前它没有:)
Font
is characteristic of a Run
object; Font
是Run
对象的特征; a run is a sequence of characters that all share the same character formatting, also loosely known as a font .一个 run 是一个字符序列,它们都共享相同的字符格式,也松散地称为font 。
A Paragraph
object also has a .font
property which is used the same way, but which specifies the default font for the runs in a paragraph. Paragraph
对象也有一个.font
属性,它的使用方式相同,但它指定了段落中运行的默认字体。 Individual runs in that paragraph can override that default by setting the attributes of their own font object.该段落中的单个运行可以通过设置自己的字体对象的属性来覆盖该默认值。
If you only want one font for the shape (which is common), probably the fastest way is:如果您只想要一种形状的字体(这是常见的),最快的方法可能是:
shape.text_frame.paragraphs[0].font.size = Pt(15)
which works because most shapes only contain a single paragraph (and all must contain at least one).这是有效的,因为大多数形状只包含一个段落(并且所有形状都必须至少包含一个)。
More thorough would be:更彻底的是:
for paragraph in shape.text_frame.paragraphs:
paragraph.font.size = Pt(15)
and more thorough still would be:更彻底的仍然是:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(15)
More details on this are in the documentation here:有关此内容的更多详细信息,请参见此处的文档:
https://python-pptx.readthedocs.io/en/latest/user/text.html https://python-pptx.readthedocs.io/en/latest/user/text.html
Here is a simple approach that worked for me:这是一个对我有用的简单方法:
slide = prs.slides.add_slide(blank_slide_layout)
slide.shapes.title.text = "The Title of My Slide"
slide.shapes.title.text_frame.paragraphs[0].font.size = Pt(15)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.