如何在 Python 中格式化文本

如何在 Python 中格式化文本

Aspose.Slides FOSS for Python 提供通过 PortionFormat 类进行细粒度文本格式化。Portion 是文本的最小独立单元;它映射到段落中的单个格式运行。本指南展示了如何对演示文稿中的文本应用粗体、斜体、字体大小和颜色格式化。

逐步指南

步骤 1:安装软件包

pip install aspose-slides-foss

步骤 2:添加带文本框的形状

在格式化文本之前,形状必须包含一个 TextFrame。使用 shape.add_text_frame() 来创建一个。

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
    tf = shape.add_text_frame("Default text: will be formatted")
    prs.save("output.pptx", SaveFormat.PPTX)

步骤 3:访问 TextFrame

shape.add_text_frame() 返回 TextFrame 对象。您也可以稍后通过 shape.text_frame 检索它。

tf = shape.text_frame          # if the frame already exists
tf = shape.add_text_frame("") # creates a new frame

一个 TextFrame 包含一个 Paragraph 对象列表(tf.paragraphs)。每个 Paragraph 包含 Portion 对象(paragraph.portions)。


步骤 4:应用粗体和斜体格式

使用 portion_format.font_boldportion_format.font_italic。这些属性接受 NullableBool.TRUENullableBool.FALSENullableBool.NOT_DEFINED(继承自 master)。

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
    tf = shape.add_text_frame("Bold and italic text")

    fmt = tf.paragraphs[0].portions[0].portion_format
    fmt.font_bold = NullableBool.TRUE
    fmt.font_italic = NullableBool.TRUE

    prs.save("bold-italic.pptx", SaveFormat.PPTX)

步骤 5:设置字体大小和颜色

portion_format.font_height 设置为大小(以点为单位),并使用 fill_format 作为颜色。

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool, FillType
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
    tf = shape.add_text_frame("Large corporate-blue heading")

    fmt = tf.paragraphs[0].portions[0].portion_format
    fmt.font_height = 32                          # 32pt font
    fmt.font_bold = NullableBool.TRUE
    fmt.fill_format.fill_type = FillType.SOLID
    fmt.fill_format.solid_fill_color.color = Color.from_argb(255, 0, 70, 127)

    prs.save("colored-text.pptx", SaveFormat.PPTX)

Color.from_argb(alpha, red, green, blue) 接受每个通道的 0–255 值。


步骤 6:一个段落中的多个部分

单个段落可以包含具有不同格式的多个部分。向段落的 portions 集合中添加一个新的 Portion

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool, FillType
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 600, 100)
    tf = shape.add_text_frame("")  # start with empty frame

    paragraph = tf.paragraphs[0]

    # First portion: normal text
    portion1 = paragraph.portions[0]
    portion1.text = "Normal text followed by "
    portion1.portion_format.font_height = 20

    # Second portion: bold red text
    portion2 = slides.Portion()
    portion2.text = "bold red text"
    portion2.portion_format.font_height = 20
    portion2.portion_format.font_bold = NullableBool.TRUE
    portion2.portion_format.fill_format.fill_type = FillType.SOLID
    portion2.portion_format.fill_format.solid_fill_color.color = Color.from_argb(255, 200, 0, 0)
    paragraph.portions.add(portion2)

    prs.save("mixed-format.pptx", SaveFormat.PPTX)

常见问题及解决方案

即使设置了颜色,文本仍显示为黑色

确保在分配颜色之前已设置 fill_format.fill_type = FillType.SOLID。如果未设置填充类型,颜色更改可能不会生效。

NullableBool.TRUE 对比 True

portion_format.font_bold 期望 NullableBool.TRUE,而不是 Python True。分配 Python True 可能会引发 TypeError,或者根据绑定的不同,什么也不做且不提示。

保存的文件中未出现字体

latin_font 属性设置拉丁字体族。如果未设置,则使用演示主题字体。自定义字体必须嵌入或在查看机器上可用。


常见问题

如何更改字体系列?

设置 portion_format.latin_font:

fmt.latin_font = slides.FontData("Arial")

FontData 接受字体族名称作为字符串。

如何设置段落对齐方式?

使用 paragraph_format.alignment:

from aspose.slides_foss import TextAlignment

tf.paragraphs[0].paragraph_format.alignment = TextAlignment.CENTER

支持的值:LEFT, CENTER, RIGHT, JUSTIFY.

我该如何设置行间距?

使用 paragraph_format.space_before(段前点)或 paragraph_format.space_after(段后点):

tf.paragraphs[0].paragraph_format.space_before = 12   # 12pt before
tf.paragraphs[0].paragraph_format.space_after = 6     # 6pt after

另请参阅

 中文