简体   繁体   English

Python-PPTX 文本格式化过程

[英]Python-PPTX Text Formatting Process

pptx** Community! pptx** 社区!

Could you please guide me if the method below is the right approach to format text?如果以下方法是格式化文本的正确方法,您能否指导我?

In other words, do I have to add: p , run , and all font properties in every text I add to the PPTX?换句话说,我是否必须在我添加到 PPTX 的每个文本中添加: prun和所有字体属性?

Thank you so much for your help!非常感谢你的帮助!

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)

To compliment Scanny's response, I have created a set of functions to apply formatting to several different objects in python-pptx .为了补充 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 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

They directly map to the logic provided by Scanny but it allows you to do the following:他们直接 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
)

To allow you to use friendly names for colours like BLUE and RED you need to have a mapping file set up or you can just pass fill_color=RGBColor(255, 255, 255) .为了允许您对BLUERED等颜色使用友好名称,您需要设置一个映射文件,或者您可以只传递fill_color=RGBColor(255, 255, 255)

No, typically not.不,通常不会。 There are some shortcuts and the defaults should work fine most of the time.有一些快捷方式,默认值在大多数情况下应该可以正常工作。

A typical shape-add operation is like this:一个典型的形状添加操作是这样的:

shape = shapes.add_shape(...)
shape.text = "foobar"

There is no need to explicitly access the text-frame to add text, a shape that accepts text has a writable .text property.无需显式访问文本框即可添加文本,接受文本的形状具有可写的.text属性。

In general, the default font (character formatting) is going to work fine.通常,默认字体(字符格式)可以正常工作。 This includes you doing work in your starting "template".pptx file to set the defaults to what you want for most of the text.这包括您在开始的“模板”.pptx 文件中进行工作,以将默认值设置为您想要的大部分文本。 These defaults would be set at the theme and presentation-defaults level, as well as in placeholders on slide-layouts.这些默认值将设置在主题和演示默认值级别,以及幻灯片布局上的占位符中。 You would typically do this by hand using PowerPoint since it's once-per-template rather than once-per-generated-presentation.您通常会使用 PowerPoint 手动执行此操作,因为它是每个模板一次,而不是每个生成的演示文稿一次。

Perhaps the most common "customization" would be to change the font size for a particular shape.也许最常见的“自定义”是更改特定形状的字体大小。 That would be:那将是:

shape.text_frame.paragraphs[0].font.size = Pt(12)

If you find yourself doing that more than occasionally, you can whip up some utility functions:如果您发现自己经常这样做,您可以启动一些实用功能:

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)

Each _Run object has a font and a certain contiguous span ("run") of characters, including possibly zero characters ("").每个_Run object 都有一个字体和一定的连续字符跨度(“run”),包括可能的零个字符(“”)。 All characters in a run share the same font and any settings you make at that level override any settings at higher levels that might otherwise be "inherited".运行中的所有字符共享相同的字体,并且您在该级别所做的任何设置都会覆盖更高级别的任何设置,否则这些设置可能会被“继承”。

Each _Paragraph object also has a .font property, but this one is the default font for runs inside that paragraph.每个_Paragraph object 也有一个.font属性,但这是该段落内运行的默认字体。 These font characteristics will apply except where the run overrides them with a setting at its level.这些字体特征将适用,除非运行使用其级别的设置覆盖它们。

So the right thing to do depends somewhat on the situation.因此,正确的做法在某种程度上取决于具体情况。 If you used the following, for example, you'll be sure to set the font for all text currently in the paragraph, but not for new text (which gets new runs) that is added:例如,如果您使用以下内容,您将确保为段落中当前的所有文本设置字体,但不会为添加的新文本(获得新的运行)设置字体:

def shape_set_font_size(shape, points):
    for paragraph in shape.text_frame.paragraphs:
        for run in paragraph.runs:
            run.font.size = Pt(points)

I recommend you avoid the _Run -level font except when you want to emphasize some particular word or phrase, say by making it bold or italic.我建议您避免使用_Run级别的字体,除非您想强调某些特定的单词或短语,例如将其设为粗体或斜体。 Otherwise the _Paragraph -level works fine and is more flexible.否则_Paragraph级别可以正常工作并且更灵活。

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

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