[英]Python-PPTX Text Formatting Process
pptx** 社区!
如果以下方法是格式化文本的正确方法,您能否指导我?
换句话说,我是否必须在我添加到 PPTX 的每个文本中添加: p
、 run
和所有字体属性?
非常感谢你的帮助!
from pptx import Presentation
from pptx.dml.color import RGBColor
from pptx.enum.dml import MSO_THEME_COLOR
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.util import Inches, Pt
prs = Presentation('./data/template.pptx')
cover = prs.slides.add_slide(prs.slide_layouts[0])
pptTitle_Shape = cover.shapes.add_textbox(Inches(0.31), Inches(0.54), 0.61, 9.38)
pptTitle_TextFrame = pptTitle_Shape.text_frame
p = pptTitle_TextFrame.paragraphs[0]
run = p.add_run()
run.text = 'Cover Page Title Name'
font = run.font
font.name = 'Arial'
font.size = Pt(40)
font.bold = False
font.color.rgb = RGBColor(255, 255, 255)
pptCustomerName_Shape = cover.shapes.add_textbox(Inches(0.31), Inches(2.03), 4.5, 0.4)
pptCustomerName_TextFrame = pptCustomerName_Shape.text_frame
p = pptCustomerName_TextFrame.paragraphs[0]
run = p.add_run()
run.text = 'Customer Name'
font = run.font
font.name = 'Arial'
font.size = Pt(28)
font.bold = False
font.color.rgb = RGBColor(255, 255, 255)
pptSEName_Shape = cover.shapes.add_textbox(Inches(0.31), Inches(2.51), 4.5, 0.4)
pptSEName_TextFrame = pptSEName_Shape.text_frame
p = pptSEName_TextFrame.paragraphs[0]
run = p.add_run()
run.text = 'FirstName LName'
font = run.font
font.name = 'Arial'
font.size = Pt(20)
font.bold = False
font.color.rgb = RGBColor(255, 255, 255)
为了补充 Scanny 的响应,我创建了一组函数来将格式应用于python-pptx
中的几个不同对象。 The functions are quite long as they implement all types of formatting like font alignment, border colours, transparency, margins etc. But you can find them in the following gist: https://gist.github.com/Fredericco72/c21505ebfb354461bc4b7b857764d5d2
他们直接 map 到 Scanny 提供的逻辑,但它允许您执行以下操作:
slide = prs.slides.add_slide(prs.slide_layouts[0])
shape = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE,
left=Cm(1),
top=Cm(1),
width=Cm(10),
height=Cm(3)
)
format_shape(
shape,
fill_color="BLUE",
border_color="RED"
)
format_text_frame(
shape.text_frame,
font_size=15,
font_color="WHITE",
font_align=PP_ALIGN.RIGHT
)
为了允许您对BLUE
和RED
等颜色使用友好名称,您需要设置一个映射文件,或者您可以只传递fill_color=RGBColor(255, 255, 255)
。
不,通常不会。 有一些快捷方式,默认值在大多数情况下应该可以正常工作。
一个典型的形状添加操作是这样的:
shape = shapes.add_shape(...)
shape.text = "foobar"
无需显式访问文本框即可添加文本,接受文本的形状具有可写的.text
属性。
通常,默认字体(字符格式)可以正常工作。 这包括您在开始的“模板”.pptx 文件中进行工作,以将默认值设置为您想要的大部分文本。 这些默认值将设置在主题和演示默认值级别,以及幻灯片布局上的占位符中。 您通常会使用 PowerPoint 手动执行此操作,因为它是每个模板一次,而不是每个生成的演示文稿一次。
也许最常见的“自定义”是更改特定形状的字体大小。 那将是:
shape.text_frame.paragraphs[0].font.size = Pt(12)
如果您发现自己经常这样做,您可以启动一些实用功能:
def shape_set_font_size(shape, points):
for paragraph in shape.text_frame.paragraphs:
paragraph.font.size = Pt(points)
shape_set_font_size(shape, 12)
shape_set_font_size(shape_2, 18)
每个_Run
object 都有一个字体和一定的连续字符跨度(“run”),包括可能的零个字符(“”)。 运行中的所有字符共享相同的字体,并且您在该级别所做的任何设置都会覆盖更高级别的任何设置,否则这些设置可能会被“继承”。
每个_Paragraph
object 也有一个.font
属性,但这是该段落内运行的默认字体。 这些字体特征将适用,除非运行使用其级别的设置覆盖它们。
因此,正确的做法在某种程度上取决于具体情况。 例如,如果您使用以下内容,您将确保为段落中当前的所有文本设置字体,但不会为添加的新文本(获得新的运行)设置字体:
def shape_set_font_size(shape, points):
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(points)
我建议您避免使用_Run
级别的字体,除非您想强调某些特定的单词或短语,例如将其设为粗体或斜体。 否则_Paragraph
级别可以正常工作并且更灵活。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.