如何在 Python 中格式化文本

如何在 Python 中格式化文本

Aspose.Slides FOSS for Python provides fine-grained text formatting through the PortionFormat 类。A 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.TRUE, NullableBool.FALSE,,或 NullableBool.NOT_DEFINED (继承自主控)。.

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:在同一段落中使用多个 Portion

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

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 vs 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

另请参阅

 中文