在 Python 中格式化演示文稿文本
概述
本文介绍如何使用 Aspose.Slides for Python via .NET 对 PowerPoint 和 OpenDocument 演示文稿中的文本进行格式化。内容包括背景颜色、透明度、字符间距、字体属性、旋转、段落间距、自动适应行为、文本锚点、制表位以及语言设置。
除非另有说明,示例均使用 sample.pptx。其第一张幻灯片的第一个形状是一个文本框,第一段包含下文所示的文本。幻灯片和形状的索引均从零开始。选择粗体部分的示例使用有效格式,包括继承的粗体格式:

要查找并突出显示字面文本或正则表达式匹配,请参阅 搜索和替换文本。
设置文本背景颜色
使用 ParagraphFormat.default_portion_format 为段落设置默认高亮颜色,或使用 BasePortionFormat.highlight_color 为单独的文本片段设置高亮颜色。
以下示例将浅灰色高亮设为第一段的默认颜色。对单独片段的显式高亮颜色可覆盖该默认值:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
# 为整个段落设置高亮颜色。
paragraph.paragraph_format.default_portion_format.highlight_color.color = draw.Color.light_gray
presentation.save("gray_paragraph.pptx", slides.export.SaveFormat.PPTX)

以下代码示例演示如何为 粗体文本片段 设置背景颜色:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
for portion in paragraph.portions:
if portion.portion_format.get_effective().font_bold:
# 为文本片段设置高亮颜色。
portion.portion_format.highlight_color.color = draw.Color.light_gray
presentation.save("gray_text_portions.pptx", slides.export.SaveFormat.PPTX)

对齐文本段落
使用 ParagraphFormat.alignment 在文本框内设置段落对齐方式。该值可以是居中、左对齐、右对齐、两端对齐等。
以下代码示例展示如何将段落对齐到 居中:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
# 将段落的对齐方式设置为居中。
paragraph.paragraph_format.alignment = slides.TextAlignment.CENTER
presentation.save("aligned_paragraph.pptx", slides.export.SaveFormat.PPTX)

在行内对齐字体
使用 ParagraphFormat.font_alignment 在一行内对不同字号的文本片段进行垂直对齐。此设置适用于整个段落,并控制每行内部的对齐方式。
以下独立示例在同一幻灯片上创建四个带标签的文本框。每个段落以 18、36、54 磅的相同文本显示,且使用不同的字体对齐方式。示例使用 Arial,禁用自动适应和换行,并保持文本框足够大以容纳单行。
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
alignments = [slides.FontAlignment.BASELINE, slides.FontAlignment.TOP, slides.FontAlignment.CENTER, slides.FontAlignment.BOTTOM]
font_sizes = [18, 36, 54]
for i, alignment in enumerate(alignments):
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 30, 20 + i * 130, 660, 120)
shape.fill_format.fill_type = slides.FillType.NO_FILL
shape.line_format.fill_format.fill_type = slides.FillType.NO_FILL
text_frame = shape.text_frame
text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.TOP
text_frame.text_frame_format.autofit_type = slides.TextAutofitType.NONE
text_frame.text_frame_format.wrap_text = slides.NullableBool.FALSE
label = text_frame.paragraphs[0]
label.text = alignment.name.title()
label.paragraph_format.alignment = slides.TextAlignment.LEFT
label.paragraph_format.default_portion_format.font_height = 14
label.paragraph_format.default_portion_format.latin_font = slides.FontData("Arial")
label.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
label.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.gray
paragraph = slides.Paragraph()
paragraph.paragraph_format.font_alignment = alignment
paragraph.paragraph_format.alignment = slides.TextAlignment.LEFT
paragraph.paragraph_format.default_portion_format.latin_font = slides.FontData("Arial")
paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
for font_size in font_sizes:
portion = slides.Portion("Ag ")
portion.portion_format.font_height = font_size
paragraph.portions.add(portion)
text_frame.paragraphs.add(paragraph)
presentation.save("font_alignment.pptx", slides.export.SaveFormat.PPTX)

字体对齐基于字体度量,因此各字符的可视边缘不一定完全对齐。示例包含大写字母和下行字,以帮助展示基线与底部对齐的差异。字体可用性和替代、使用的字符以及字号差异都会影响结果。框架尺寸、边距、行距、换行和自动适应也会影响布局;比较不同模式时请使用相同的字体和布局设置。
此设置不同于 ParagraphFormat.alignment,后者控制段落的水平对齐;也不同于 TextFrameFormat.anchoring_type,后者在形状内部垂直定位文本块。通过 BasePortionFormat.escapement 实现的上标和下标格式会相对于基线移动单个片段,而不是为段落的行设置字体对齐。
设置文本透明度
文本透明度通过分配给 BasePortionFormat.fill_format 的颜色的 alpha 分量来控制。在下面的示例中,alpha = 50 是 0–255 规模的 ARGB alpha 通道值,而非透明度百分比。
以下代码示例展示如何对 整个段落 应用透明度:
import aspose.pydrawing as draw
import aspose.slides as slides
alpha = 50
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
# 为文本设置半透明黑色填充。
paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.from_argb(alpha, draw.Color.black)
presentation.save("transparent_paragraph.pptx", slides.export.SaveFormat.PPTX)

以下代码示例展示如何对 粗体文本片段 应用透明度:
import aspose.pydrawing as draw
import aspose.slides as slides
alpha = 50
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
for portion in paragraph.portions:
if portion.portion_format.get_effective().font_bold:
# 为文本片段设置透明度。
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
portion.portion_format.fill_format.solid_fill_color.color = draw.Color.from_argb(alpha, draw.Color.black)
presentation.save("transparent_text_portions.pptx", slides.export.SaveFormat.PPTX)

设置文本字符间距
使用 BasePortionFormat.spacing 来扩大或压缩文本框中字符之间的间距。示例中添加了 3 磅的间距;负值会压缩文本。
以下 Python 代码展示如何在 整个段落 中扩大字符间距:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
# 注意:使用负值可以压缩字符间距。
paragraph.paragraph_format.default_portion_format.spacing = 3 # 扩展字符间距。
presentation.save("character_spacing_in_paragraph.pptx", slides.export.SaveFormat.PPTX)

以下代码示例展示如何在 粗体文本片段 中扩大字符间距:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
for portion in paragraph.portions:
if portion.portion_format.get_effective().font_bold:
# 注意:使用负值可以压缩字符间距。
portion.portion_format.spacing = 3 # 扩展字符间距。
presentation.save("character_spacing_in_text_portions.pptx", slides.export.SaveFormat.PPTX)

为特定字体禁用字距调整
在某些情况下,Aspose.Slides 渲染的文本可能比 PowerPoint 中显示的相同文本略紧。这可能是因为 PowerPoint 会忽略某些字体的字距信息,即使该字体包含有效的字距数据且在 PowerPoint 设置中已启用字距调整。
为了使渲染输出更接近 PowerPoint,可以对使用受影响字体的文本片段禁用字距调整。将 BasePortionFormat.kerning_minimal_size 设置为大于实际字号的值。本示例需要在第一张幻灯片的第一个形状中有文本框的 “presentation.pptx”。它检查有效的字体名称(包括继承的字体),并为使用 Roboto 的片段设置 100 磅的阈值。这会对字号低于 100 磅的匹配片段禁用字距调整:
import aspose.slides as slides
with slides.Presentation("presentation.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
target_font = "Roboto"
for paragraph in auto_shape.text_frame.paragraphs:
for portion in paragraph.portions:
text_format = portion.portion_format.get_effective()
fonts = (text_format.latin_font, text_format.east_asian_font, text_format.complex_script_font)
uses_target_font = any(font is not None and font.font_name == target_font for font in fonts)
if uses_target_font:
portion.portion_format.kerning_minimal_size = 100
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
对低于阈值的匹配文本,此设置会阻止字距调整,从而帮助 Aspose.Slides 的渲染效果与 PowerPoint 在受此 PowerPoint 特定行为影响的字体上保持一致。
管理文本字体属性
字体属性可以通过 ParagraphFormat.default_portion_format 在段落层面设置,也可以通过 PortionFormat 在单独的片段上设置。
以下示例将第一段的默认字体设为 12 磅 Times New Roman,并使用粗体、斜体和点状下划线格式。对单独片段的显式格式会覆盖这些默认设置。
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
# 为段落设置字体属性。
portion_format = paragraph.paragraph_format.default_portion_format
portion_format.font_height = 12
portion_format.font_bold = slides.NullableBool.TRUE
portion_format.font_italic = slides.NullableBool.TRUE
portion_format.font_underline = slides.TextUnderlineType.DOTTED
portion_format.latin_font = slides.FontData("Times New Roman")
presentation.save("font_properties_for_paragraph.pptx", slides.export.SaveFormat.PPTX)

以下示例对有效格式为粗体的片段应用 13 磅 Times New Roman、斜体和点状下划线:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
for portion in paragraph.portions:
if portion.portion_format.get_effective().font_bold:
# 为文本片段设置字体属性。
portion.portion_format.font_height = 13
portion.portion_format.font_italic = slides.NullableBool.TRUE
portion.portion_format.font_underline = slides.TextUnderlineType.DOTTED
portion.portion_format.latin_font = slides.FontData("Times New Roman")
presentation.save("font_properties_for_text_portions.pptx", slides.export.SaveFormat.PPTX)

控制文本高度标准化
在复现模板排版或研究字符高度差异时使用 BasePortionFormat.normalise_height。在 PowerPoint 中,启用标准化会垂直拉伸或压缩单个字符,使其在文本片段内具有统一的可视高度。但这并不保证不同字体的文本高度相等。
将该属性设置为 NullableBool.TRUE or FALSE 可显式启用或禁用标准化。NullableBool.NOT_DEFINED 保持本地设置未定义,使片段继承其值,例如来自段落的 default_portion_format。它并不等同于 False。使用 PortionFormat.get_effective 并读取 IBasePortionFormatEffectiveData.normalise_height 可获取解析后的布尔值。
以下独立示例需要安装 Arial。它创建两个包含相同大写字母、小写字母、下行字和带重音字符(54 磅)的文本块。两个段落默认启用标准化。顶部片段显式禁用;底部片段继承设置。框架尺寸、边距和段落间距相同,且禁用换行和自动适应以保持比较一致。示例打印 FALSE: False 和 NOT_DEFINED: True,随后保存为 “text_height_normalization.pptx”。
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
settings = [slides.NullableBool.FALSE, slides.NullableBool.NOT_DEFINED]
for i, setting in enumerate(settings):
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 30, 60 + i * 200, 660, 150)
shape.fill_format.fill_type = slides.FillType.NO_FILL
shape.line_format.fill_format.fill_type = slides.FillType.NO_FILL
text_frame = shape.text_frame
text_frame.text = "Hxgjpq ÅÉ 0123"
text_frame.text_frame_format.autofit_type = slides.TextAutofitType.NONE
text_frame.text_frame_format.wrap_text = slides.NullableBool.FALSE
text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.TOP
text_frame.text_frame_format.margin_top = 0
text_frame.text_frame_format.margin_left = 0
paragraph = text_frame.paragraphs[0]
paragraph.paragraph_format.alignment = slides.TextAlignment.LEFT
paragraph.paragraph_format.space_before = 0
paragraph.paragraph_format.space_after = 0
paragraph.paragraph_format.space_within = 100
paragraph.paragraph_format.default_portion_format.normalise_height = slides.NullableBool.TRUE
portion = paragraph.portions[0]
portion.portion_format.latin_font = slides.FontData("Arial")
portion.portion_format.font_height = 54
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
portion.portion_format.fill_format.solid_fill_color.color = "black"
portion.portion_format.normalise_height = setting
print(f"{setting.name}: {portion.portion_format.get_effective().normalise_height}")
presentation.save("text_height_normalization.pptx", slides.export.SaveFormat.PPTX)
下面的比较图来自已保存的演示文稿。顶部行的标准化已禁用(FALSE);底部行继承了已启用的设置(NOT_DEFINED → True)。请保持安装 Arial 以重现此外观。

标准化会改变字符高度但不修改分配的 font_height 值,该值指定字体大小(磅)。段落的 space_within 控制行间距,而文本框的 autofit_type 控制文本或其容器如何自动适应。这些设置各有用途,可独立影响整体布局。有关段落格式和继承的更多信息,请参阅 Manage Paragraph。
应用和移除删除线
在文本片段上使用 BasePortionFormat.strikethrough_type 可在不删除原始文本的情况下显示纠正、作废的词语或以前的价格。将其设置为 TextStrikethroughType.SINGLE or DOUBLE 可在文本上绘制一条或两条删除线。
要移除删除线,请赋值 TextStrikethroughType.NONE。这会显式禁用该效果,即使周围的格式指定了删除线。相反,TextStrikethroughType.NOT_DEFINED 将本地设置保持未定义,使片段能够继承格式,例如来自段落。它并不保证删除线被移除。使用 PortionFormat.get_effective 可读取最终的格式,包括继承的值。
以下独立示例创建了三个价格比较。每个段落的标签、原价和现价分别放在不同的片段中,因此仅原价会受影响。第一行先应用后移除删除线;其余行保留单线和双线删除线。修改此属性可保留文本及其其他格式,包括粗体的原价。
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 50, 50, 620, 220)
shape.fill_format.fill_type = slides.FillType.NO_FILL
shape.line_format.fill_format.fill_type = slides.FillType.NO_FILL
shape.text_frame.paragraphs.clear()
labels = ["None: ", "Single: ", "Double: "]
for label in labels:
paragraph = slides.Paragraph()
paragraph.paragraph_format.alignment = slides.TextAlignment.LEFT
paragraph.paragraph_format.space_after = 20
portion_format = paragraph.paragraph_format.default_portion_format
portion_format.font_height = 28
portion_format.latin_font = slides.FontData("Arial")
portion_format.fill_format.fill_type = slides.FillType.SOLID
portion_format.fill_format.solid_fill_color.color = draw.Color.black
paragraph.portions.add(slides.Portion(label))
previous_price = slides.Portion("$100")
previous_price.portion_format.font_bold = slides.NullableBool.TRUE
paragraph.portions.add(previous_price)
paragraph.portions.add(slides.Portion(" → $80"))
shape.text_frame.paragraphs.add(paragraph)
paragraphs = shape.text_frame.paragraphs
# 先应用删除线,然后显式地从第一个原价中移除它。
paragraphs[0].portions[1].portion_format.strikethrough_type = slides.TextStrikethroughType.SINGLE
paragraphs[0].portions[1].portion_format.strikethrough_type = slides.TextStrikethroughType.NONE
paragraphs[1].portions[1].portion_format.strikethrough_type = slides.TextStrikethroughType.SINGLE
paragraphs[2].portions[1].portion_format.strikethrough_type = slides.TextStrikethroughType.DOUBLE
presentation.save("strikethrough.pptx", slides.export.SaveFormat.PPTX)
结果(从上到下):无删除线、单线删除线和双线删除线。

设置文本旋转
使用 TextFrameFormat.text_vertical_type 在形状内设置预定义的文本方向。
以下代码示例将形状内的文本方向设为 TextVerticalType.VERTICAL270,即将文本 逆时针旋转 90 度:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
auto_shape.text_frame.text_frame_format.text_vertical_type = slides.TextVerticalType.VERTICAL270
presentation.save("text_rotation.pptx", slides.export.SaveFormat.PPTX)

为文本框设置自定义旋转
使用 TextFrameFormat.rotation_angle 为 TextFrame 设置自定义旋转角度。
以下代码示将在形状内的文本框顺时针旋转 3 度:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
auto_shape.text_frame.text_frame_format.rotation_angle = 3
presentation.save("custom_text_rotation.pptx", slides.export.SaveFormat.PPTX)

设置段落行距
Aspose.Slides 提供 ParagraphFormat.space_after、ParagraphFormat.space_before 和 ParagraphFormat.space_within 来控制段落间距。其用法如下:
- 使用正值将行距指定为行高的百分比。
- 使用负值将行距指定为磅值。
以下示例将第一段的内部间距设为行高的 200%(双倍行距):
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
paragraph.paragraph_format.space_within = 200
presentation.save("line_spacing.pptx", slides.export.SaveFormat.PPTX)

控制换行
段落换行规则在狭窄文本块以及混合拉丁文和东亚文字的演示文稿中很有用。以下属性属于 ParagraphFormat,因此适用于整个段落:
- latin_line_break 控制拉丁文字的换行规则。对混合文本进行更改时,也会影响相邻的东亚文字和标点的换行位置。
- east_asian_line_break 控制东亚文字的换行规则,包括对行首和行尾字符的限制。
这些规则并不取代 TextFrameFormat.wrap_text,该设置在文本框内启用自动换行。它们在换行时影响布局,但不会插入换行符。显式换行会独立于可用宽度在段落中强制新建一行。
以下独立示例创建一个包含中文和拉丁文字的狭窄文本块。它显式设置了两项换行属性并保存为 “line_breaking.pptx”。要实验任意规则,只需更改对应属性的值,同时保持其他设置不变。示例使用 24 磅 Arial 和 SimSun,框宽 160 磅,水平文本框边距为零。将 TextFrameFormat.autofit_type 设置为 TextAutofitType.NONE,以使文本大小和框架尺寸保持固定。
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 50, 50, 160, 300)
shape.fill_format.fill_type = slides.FillType.NO_FILL
text_frame = shape.text_frame
text_frame.text_frame_format.wrap_text = slides.NullableBool.TRUE
text_frame.text_frame_format.autofit_type = slides.TextAutofitType.NONE
text_frame.text_frame_format.margin_left = 0
text_frame.text_frame_format.margin_right = 0
paragraph = text_frame.paragraphs[0]
paragraph.text = "中文排版测试,PowerPoint 中文演示。"
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = slides.TextAlignment.LEFT
paragraph_format.default_portion_format.font_height = 24
paragraph_format.default_portion_format.latin_font = slides.FontData("Arial")
paragraph_format.default_portion_format.east_asian_font = slides.FontData("SimSun")
paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
paragraph_format.latin_line_break = slides.NullableBool.FALSE
paragraph_format.east_asian_line_break = slides.NullableBool.TRUE
presentation.save("line_breaking.pptx", slides.export.SaveFormat.PPTX)
控制悬挂标点
ParagraphFormat.hanging_punctuation 允许符合条件的标点超出文本行的右边缘,而不是占据下一行。该设置适用于整个段落,与悬挂缩进不同。
以下独立示例在宽度为 100 磅的文本框中启用悬挂标点,并保存为 “hanging_punctuation.pptx”。使用 24 磅 Arial 且水平文本框边距为零时,句末的句点仍位于 “sentence” 之后并超出文本右边缘。将属性设为 NullableBool.FALSE 可进行对比:此设置下,句点会占据单独一行。启用换行并禁用自动适应,以保持可用宽度固定。
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 50, 50, 100, 200)
shape.fill_format.fill_type = slides.FillType.NO_FILL
text_frame = shape.text_frame
text_frame.text_frame_format.wrap_text = slides.NullableBool.TRUE
text_frame.text_frame_format.autofit_type = slides.TextAutofitType.NONE
text_frame.text_frame_format.margin_left = 0
text_frame.text_frame_format.margin_right = 0
paragraph = text_frame.paragraphs[0]
paragraph.text = "Simple text, next sentence."
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = slides.TextAlignment.LEFT
paragraph_format.default_portion_format.font_height = 24
paragraph_format.default_portion_format.latin_font = slides.FontData("Arial")
paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
paragraph_format.hanging_punctuation = slides.NullableBool.TRUE
presentation.save("hanging_punctuation.pptx", slides.export.SaveFormat.PPTX)
并非所有标点都能悬挂。可见结果取决于 font and layout conditions:更改字体、可用宽度、边距或自动适应设置都可能消除可见差异。
设置文本框的自动适应类型
TextFrameFormat.autofit_type 决定文本超出容器边界时的行为。使用它可以控制文本是缩小、溢出还是自动调整形状大小。以下示例将形状配置为根据文本自动调整大小,并将结果保存为 “autofit_type.pptx”。
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
auto_shape.text_frame.text_frame_format.autofit_type = slides.TextAutofitType.SHAPE
presentation.save("autofit_type.pptx", slides.export.SaveFormat.PPTX)
要在自动换行后统计行数并查看文本或形状宽度的变化,请参阅 Count Rendered Lines。仅凭行数并不能判断文本是否溢出其容器。
设置文本框的锚点
TextFrameFormat.anchoring_type 定义文本在形状内部的垂直定位方式,例如位于顶部、居中或底部。以下示例将文本锚定在第一个形状的底部,并将结果保存为 “text_anchor.pptx”。
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
auto_shape.text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.BOTTOM
presentation.save("text_anchor.pptx", slides.export.SaveFormat.PPTX)
设置文本制表位
使用 ParagraphFormat.default_tab_size 和 ParagraphFormat.tabs 配置段落的制表位。以下示例将默认制表间距设为 100 磅,并在 30 磅处添加左对齐的制表位。这些设置会影响包含制表符的文本。
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
paragraph.paragraph_format.default_tab_size = 100
paragraph.paragraph_format.tabs.add(30, slides.TabAlignment.LEFT)
presentation.save("paragraph_tabs.pptx", slides.export.SaveFormat.PPTX)

设置校对语言
Aspose.Slides 提供 BasePortionFormat.language_id,允许为文本片段设置校对语言。校对语言决定 PowerPoint 中拼写和语法检查使用的语言。
以下示例需要在第一张幻灯片的第一个形状中有文本框的 “presentation.pptx”,且至少包含一个段落。示例将第一段的内容替换为 “1。”,将其字体设为 SimSun,并分配简体中文校对语言(zh-CN)。结果保存为 “proofing_language.pptx”:
import aspose.slides as slides
with slides.Presentation("presentation.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
paragraph.portions.clear()
font = slides.FontData("SimSun")
text_portion = slides.Portion()
text_portion.portion_format.complex_script_font = font
text_portion.portion_format.east_asian_font = font
text_portion.portion_format.latin_font = font
# 将校对语言设置为简体中文。
text_portion.portion_format.language_id = "zh-CN"
text_portion.text = "1。"
paragraph.portions.add(text_portion)
presentation.save("proofing_language.pptx", slides.export.SaveFormat.PPTX)
设置默认语言
使用 LoadOptions.default_text_language 定义在加载或创建演示文稿时创建的文本的默认语言。以下示例创建一个默认文本语言为美式英语的演示文稿,添加一个文本框,并打印其第一个文本片段的语言代码 en-US。
import aspose.slides as slides
load_options = slides.LoadOptions()
load_options.default_text_language = "en-US"
with slides.Presentation(load_options) as presentation:
slide = presentation.slides[0]
# 添加一个带文本的矩形形状。
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 150, 50)
shape.text_frame.text = "Sample text"
# 检查第一个片段的语言。
portion = shape.text_frame.paragraphs[0].portions[0]
print(portion.portion_format.language_id)
设置默认文本样式
要在演示文稿级别应用默认文本格式,请使用 Presentation.default_text_style。
以下示例为新演示文稿中顶层段落设置 14 磅粗体字体作为默认,并保存为 “default_text_style.pptx”。文本可以继承这些默认值,除非更具体的格式覆盖了它们。
import aspose.slides as slides
with slides.Presentation() as presentation:
# 获取顶层段落格式。
paragraph_format = presentation.default_text_style.get_level(0)
if paragraph_format is not None:
paragraph_format.default_portion_format.font_height = 14
paragraph_format.default_portion_format.font_bold = slides.NullableBool.TRUE
presentation.save("default_text_style.pptx", slides.export.SaveFormat.PPTX)
提取带全大写效果的文本
在 PowerPoint 中,应用 All Caps 字体效果会使文本在幻灯片上显示为大写,即使原始输入为小写。使用 Aspose.Slides 检索此类文本片段时,库会返回原始输入的文本。若要匹配显示的文本,需要检查 TextCapType 并在其值为 ALL 时将返回的字符串转换为大写。
此示例需要在第一张幻灯片的第一个形状中有文本框的 “sample2.pptx”。其第一段的首个片段包含带有 All Caps 效果的 “Hello, Aspose!” ,如下所示。

以下代码示例展示如何提取带有 All Caps 效果的文本:
import aspose.slides as slides
with slides.Presentation("sample2.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
text_portion = auto_shape.text_frame.paragraphs[0].portions[0]
print("Original text:", text_portion.text)
text_format = text_portion.portion_format.get_effective()
if text_format.text_cap_type == slides.TextCapType.ALL:
text = text_portion.text.upper()
print("All-Caps effect:", text)
输出:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
常见问题
如何修改幻灯片表格中的文本?
要修改幻灯片表格中的文本,请使用 Table。遍历单元格,并通过 Cell.text_frame 更新每个单元格的文本,并通过 Paragraph.paragraph_format 设置段落格式。
如何在 PowerPoint 幻灯片上的文本应用渐变颜色?
要对文本应用渐变颜色,请使用 BasePortionFormat.fill_format。将 FillFormat.fill_type 设置为 FillType.GRADIENT,并配置渐变点、方向和透明度。
相关功能:在幻灯片中格式化文本