Luk*_*low 6 python python-pptx
我想更改我的 pptx 演示文稿的标题和正文的字体大小。title_shape.font = Pt(15)我尝试通过和设置它body_shape.font = Pt(10),但不起作用。
这是我的代码:
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')
Run Code Online (Sandbox Code Playgroud)
对象Shape没有.font属性(至少在您通过分配该名称来添加属性之前它没有:)
Font是Run物体的特征;串是共享相同字符格式的字符序列,也被广泛称为字体。
对象还有一个属性Paragraph,其使用方式相同,但它指定段落中的默认字体。该段落中的各个运行可以通过设置自己的字体对象的属性来覆盖默认值。.font
如果您只想要一种形状字体(这很常见),最快的方法可能是:
shape.text_frame.paragraphs[0].font.size = Pt(15)
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为大多数形状只包含一个段落(并且所有形状都必须至少包含一个段落)。
更彻底的是:
for paragraph in shape.text_frame.paragraphs:
paragraph.font.size = Pt(15)
Run Code Online (Sandbox Code Playgroud)
更彻底的仍然是:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(15)
Run Code Online (Sandbox Code Playgroud)
有关此内容的更多详细信息,请参阅此处的文档: https:
//python-pptx.readthedocs.io/en/latest/user/text.html