在 Python 中管理 PowerPoint 文本段落
概述
Aspose.Slides for Python via .NET 将文本表示为文本框、段落和片段的层次结构:
- TextFrame 表示形状中的文本容器,并提供对其段落集合的访问。
- Paragraph 表示文本框中的一个段落,并提供对其片段和段落级格式的访问。
- Portion 表示段落中的一段文本。每个片段可以拥有自己的文本和字符级格式。
因此,一个段落可以通过使用多个片段来包含不同字体、颜色、大小和其他格式的文本。
创建和格式化段落
使用多个片段创建段落
以下步骤创建一个包含三个段落、每个段落包含三个片段的文本框:
- 创建 Presentation 类的实例。
- 通过索引访问相应的幻灯片。
- 向幻灯片添加一个矩形的 AutoShape。
- 获取形状的 TextFrame。
- 使用默认段落,并向文本框再添加两个 Paragraph 对象。
- 为每个段落添加足够的 Portion 对象,使其包含三个片段。默认段落已经包含一个空片段。
- 设置每个片段的文本。
- 通过 Portion.portion_format 应用字符级格式设置。
- 保存修改后的演示文稿。
此 Python 示例实现了上述步骤:
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, 150, 300, 150)
text_frame = shape.text_frame
first_paragraph = text_frame.paragraphs[0]
first_paragraph.portions.add(slides.Portion())
first_paragraph.portions.add(slides.Portion())
second_paragraph = slides.Paragraph()
second_paragraph.portions.add(slides.Portion())
second_paragraph.portions.add(slides.Portion())
second_paragraph.portions.add(slides.Portion())
text_frame.paragraphs.add(second_paragraph)
third_paragraph = slides.Paragraph()
third_paragraph.portions.add(slides.Portion())
third_paragraph.portions.add(slides.Portion())
third_paragraph.portions.add(slides.Portion())
text_frame.paragraphs.add(third_paragraph)
for paragraph_index in range(text_frame.paragraphs.count):
paragraph = text_frame.paragraphs[paragraph_index]
for portion_index in range(paragraph.portions.count):
portion = paragraph.portions[portion_index]
portion.text = f"Portion {paragraph_index + 1}.{portion_index + 1}"
if portion_index == 0:
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
portion.portion_format.fill_format.solid_fill_color.color = draw.Color.red
portion.portion_format.font_bold = slides.NullableBool.TRUE
portion.portion_format.font_height = 15
elif portion_index == 1:
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
portion.portion_format.fill_format.solid_fill_color.color = draw.Color.blue
portion.portion_format.font_italic = slides.NullableBool.TRUE
portion.portion_format.font_height = 18
presentation.save("paragraphs_with_portions.pptx", slides.export.SaveFormat.PPTX)
创建项目符号和编号列表
创建项目符号或编号列表
项目符号和编号使相关项目更易于浏览。在 Aspose.Slides 中,列表设置通过 BulletFormat 定义。
- 创建 Presentation 类的实例。
- 通过索引访问相应的幻灯片。
- 向选定的幻灯片添加一个 AutoShape。
- 获取形状的 TextFrame。
- 从文本框中移除默认段落。
- 为符号项目符号创建一个 Paragraph。
- 将 BulletFormat.type 设置为 BulletType.SYMBOL 并指定项目符号字符。
- 设置段落文本、缩进、项目符号颜色和项目符号高度。
- 将段落添加到文本框。
- 创建第二个段落,并将 BulletFormat.type 设置为 BulletType.NUMBERED。
- 配置编号项目符号样式并将段落添加到文本框。
- 保存演示文稿。
此 Python 示例创建了符号项目符号和编号项目符号:
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, 200, 200, 400, 200)
text_frame = shape.text_frame
text_frame.paragraphs.clear()
symbol_paragraph = slides.Paragraph()
symbol_paragraph.text = "Welcome to Aspose.Slides"
symbol_paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
symbol_paragraph.paragraph_format.bullet.char = chr(0x2022)
symbol_paragraph.paragraph_format.indent = 25
symbol_paragraph.paragraph_format.bullet.color.color_type = slides.ColorType.RGB
symbol_paragraph.paragraph_format.bullet.color.color = draw.Color.black
symbol_paragraph.paragraph_format.bullet.is_bullet_hard_color = slides.NullableBool.TRUE
symbol_paragraph.paragraph_format.bullet.height = 100
text_frame.paragraphs.add(symbol_paragraph)
numbered_paragraph = slides.Paragraph()
numbered_paragraph.text = "This is a numbered item"
numbered_paragraph.paragraph_format.bullet.type = slides.BulletType.NUMBERED
numbered_paragraph.paragraph_format.bullet.numbered_bullet_style = slides.NumberedBulletStyle.BULLET_CIRCLE_NUM_WD_BLACK_PLAIN
numbered_paragraph.paragraph_format.indent = 25
numbered_paragraph.paragraph_format.bullet.color.color_type = slides.ColorType.RGB
numbered_paragraph.paragraph_format.bullet.color.color = draw.Color.black
numbered_paragraph.paragraph_format.bullet.is_bullet_hard_color = slides.NullableBool.TRUE
numbered_paragraph.paragraph_format.bullet.height = 100
text_frame.paragraphs.add(numbered_paragraph)
presentation.save("bulleted_and_numbered_list.pptx", slides.export.SaveFormat.PPTX)
使用图片项目符号
图片项目符号允许使用自定义图片代替符号或数字。
- 创建 Presentation 类的实例。
- 通过索引访问相应的幻灯片。
- 添加一个 AutoShape 并获取其 TextFrame。
- 从文本框中移除默认段落。
- 加载项目符号图片并将其作为 PPImage 添加到演示文稿的图像集合中。
- 创建一个 Paragraph 并设置其文本。
- 将 BulletFormat.type 设置为 BulletType.PICTURE。
- 通过 BulletFormat.picture 指定图片并设置项目符号高度。
- 将段落添加到文本框。
- 保存修改后的演示文稿。
此 Python 示例创建了图片项目符号:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
with slides.Images.from_file("bullets.png") as bullet_image:
presentation_image = presentation.images.add_image(bullet_image)
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 200, 200, 400, 200)
text_frame = shape.text_frame
text_frame.paragraphs.clear()
paragraph = slides.Paragraph()
paragraph.text = "Welcome to Aspose.Slides"
paragraph.paragraph_format.bullet.type = slides.BulletType.PICTURE
paragraph.paragraph_format.bullet.picture.image = presentation_image
paragraph.paragraph_format.bullet.height = 100
text_frame.paragraphs.add(paragraph)
presentation.save("picture_bullet.pptx", slides.export.SaveFormat.PPTX)
presentation.save("picture_bullet.ppt", slides.export.SaveFormat.PPT)
创建多级列表
将 ParagraphFormat.depth 设置为不同的深度,以在列表中放置不同层级的段落。最高层的深度为 0。
- 创建一个 Presentation 并访问一张幻灯片。
- 添加一个 AutoShape 并清除其文本框中的默认段落。
- 创建四个段落并配置它们的项目符号符号。
- 将它们的 ParagraphFormat.depth 值分别设为
0、1、2、3。 - 将段落添加到文本框并保存演示文稿。
此 Python 示例创建了四层项目符号列表:
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, 200, 200, 400, 200)
text_frame = shape.text_frame
text_frame.paragraphs.clear()
first_paragraph = slides.Paragraph()
first_paragraph.text = "Content"
first_paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
first_paragraph.paragraph_format.bullet.char = chr(0x2022)
first_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
first_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
first_paragraph.paragraph_format.depth = 0
second_paragraph = slides.Paragraph()
second_paragraph.text = "Second level"
second_paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
second_paragraph.paragraph_format.bullet.char = "-"
second_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
second_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
second_paragraph.paragraph_format.depth = 1
third_paragraph = slides.Paragraph()
third_paragraph.text = "Third level"
third_paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
third_paragraph.paragraph_format.bullet.char = chr(0x2022)
third_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
third_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
third_paragraph.paragraph_format.depth = 2
fourth_paragraph = slides.Paragraph()
fourth_paragraph.text = "Fourth level"
fourth_paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
fourth_paragraph.paragraph_format.bullet.char = "-"
fourth_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
fourth_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
fourth_paragraph.paragraph_format.depth = 3
text_frame.paragraphs.add(first_paragraph)
text_frame.paragraphs.add(second_paragraph)
text_frame.paragraphs.add(third_paragraph)
text_frame.paragraphs.add(fourth_paragraph)
presentation.save("multilevel_list.pptx", slides.export.SaveFormat.PPTX)
为编号列表项设置自定义起始值
使用 BulletFormat.numbered_bullet_start_with 可为编号段落设置初始显示数字。
- 创建一个 Presentation 并向幻灯片添加一个 AutoShape。
- 清除形状文本框中的默认段落。
- 创建三个编号段落。
- 将 BulletFormat.numbered_bullet_start_with 分别设置为
2、3、7。 - 将段落添加到文本框并保存演示文稿。
此 Python 示例为每个段落分配自定义起始编号:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 200, 200, 400, 200)
text_frame = shape.text_frame
text_frame.paragraphs.clear()
first_paragraph = slides.Paragraph()
first_paragraph.text = "Start at 2"
first_paragraph.paragraph_format.bullet.type = slides.BulletType.NUMBERED
first_paragraph.paragraph_format.bullet.numbered_bullet_start_with = 2
text_frame.paragraphs.add(first_paragraph)
second_paragraph = slides.Paragraph()
second_paragraph.text = "Start at 3"
second_paragraph.paragraph_format.bullet.type = slides.BulletType.NUMBERED
second_paragraph.paragraph_format.bullet.numbered_bullet_start_with = 3
text_frame.paragraphs.add(second_paragraph)
third_paragraph = slides.Paragraph()
third_paragraph.text = "Start at 7"
third_paragraph.paragraph_format.bullet.type = slides.BulletType.NUMBERED
third_paragraph.paragraph_format.bullet.numbered_bullet_start_with = 7
text_frame.paragraphs.add(third_paragraph)
presentation.save("custom_numbered_list.pptx", slides.export.SaveFormat.PPTX)
控制段落布局和结尾属性
设置首行缩进
使用 ParagraphFormat.indent 属性来控制段落的首行缩进。该属性仅移动首行相对于段落左边距的位置。正值将首行向右移动,而其余行保持与段落主体对齐。
当需要移动整个段落时,请使用 ParagraphFormat.margin_left。仅需移动首行时,请使用 ParagraphFormat.indent。
下面的示例创建了多个段落,并对不同的 ParagraphFormat.indent 值进行演示,以展示首行缩进如何影响段落布局。
- 创建 Presentation 类的实例。
- 访问目标幻灯片。
- 向幻灯片添加一个矩形的 AutoShape。
- 获取形状的 TextFrame 并移除默认段落。
- 创建多个段落并为它们设置不同的 ParagraphFormat.indent 值。
- 将段落添加到文本框。
- 保存修改后的演示文稿。
此代码展示了如何设置段落缩进:
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, 420, 220)
shape.fill_format.fill_type = slides.FillType.NO_FILL
shape.line_format.fill_format.fill_type = slides.FillType.SOLID
shape.line_format.fill_format.solid_fill_color.color = draw.Color.gray
text_frame = shape.text_frame
text_frame.text_frame_format.autofit_type = slides.TextAutofitType.SHAPE
text_frame.paragraphs.clear()
first_paragraph = slides.Paragraph()
first_paragraph.text = "No first-line indent. Wrapped lines start at the same position as the first line."
first_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
first_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
first_paragraph.paragraph_format.margin_left = 20
first_paragraph.paragraph_format.indent = 0
second_paragraph = slides.Paragraph()
second_paragraph.text = "First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body."
second_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
second_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
second_paragraph.paragraph_format.margin_left = 20
second_paragraph.paragraph_format.indent = 20
third_paragraph = slides.Paragraph()
third_paragraph.text = "First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see."
third_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
third_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
third_paragraph.paragraph_format.margin_left = 20
third_paragraph.paragraph_format.indent = 40
text_frame.paragraphs.add(first_paragraph)
text_frame.paragraphs.add(second_paragraph)
text_frame.paragraphs.add(third_paragraph)
presentation.save("paragraph_indent.pptx", slides.export.SaveFormat.PPTX)
结果:

设置悬挂缩进
悬挂缩进是一种段落布局,使首行位于其余行的左侧。在 Aspose.Slides 中,可通过 ParagraphFormat.indent 属性实现。将 indent 设为负值即可将首行相对于段落正文向左移动。
实际使用中, ParagraphFormat.margin_left 定义段落正文的左侧位置,而 ParagraphFormat.indent 定义首行相对于该左侧边距的位置。要创建悬挂缩进,需将 margin_left 设置为正值,同时将 indent 设置为负值。
此格式在参考文献、词汇表条目以及其他需要换行后对齐到段落正文而非首字符的段落中非常有用。
- 创建 Presentation 类的实例。
- 访问目标幻灯片。
- 向幻灯片添加一个矩形的 AutoShape。
- 获取形状的 TextFrame 并移除默认段落。
- 创建段落并为每个段落设置正的 ParagraphFormat.margin_left 值。
- 将负的 ParagraphFormat.indent 值用于实现悬挂缩进效果。
- 将段落添加到文本框。
- 保存修改后的演示文稿。
此代码展示了如何为段落设置悬挂缩进:
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, 420, 220)
shape.fill_format.fill_type = slides.FillType.NO_FILL
shape.line_format.fill_format.fill_type = slides.FillType.SOLID
shape.line_format.fill_format.solid_fill_color.color = draw.Color.gray
text_frame = shape.text_frame
text_frame.text_frame_format.autofit_type = slides.TextAutofitType.SHAPE
text_frame.paragraphs.clear()
first_paragraph = slides.Paragraph()
first_paragraph.text = "A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body."
first_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
first_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
first_paragraph.paragraph_format.margin_left = 40
first_paragraph.paragraph_format.indent = -20
second_paragraph = slides.Paragraph()
second_paragraph.text = "This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare."
second_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
second_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
second_paragraph.paragraph_format.margin_left = 60
second_paragraph.paragraph_format.indent = -30
text_frame.paragraphs.add(first_paragraph)
text_frame.paragraphs.add(second_paragraph)
presentation.save("hanging_indent.pptx", slides.export.SaveFormat.PPTX)
结果:

设置段落结束运行属性
Paragraph.end_paragraph_portion_format 属性控制段落结束标记的格式。下面的示例为第二段落的结束标记分配字体大小和拉丁字体:
- 加载一个 Presentation 并访问一张幻灯片。
- 添加一个 AutoShape 并清除其默认段落。
- 创建两个段落并向其中添加文本片段。
- 为第二段落的结束标记创建一个 PortionFormat。
- 设置 PortionFormat.font_height 和 PortionFormat.latin_font。
- 将该格式分配给 Paragraph.end_paragraph_portion_format 并保存演示文稿。
import aspose.slides as slides
with slides.Presentation("Test.pptx") as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 10, 200, 250)
text_frame = shape.text_frame
text_frame.paragraphs.clear()
first_paragraph = slides.Paragraph()
first_paragraph.portions.add(slides.Portion("Sample text"))
second_paragraph = slides.Paragraph()
second_paragraph.portions.add(slides.Portion("Sample text 2"))
end_paragraph_format = slides.PortionFormat()
end_paragraph_format.font_height = 48
end_paragraph_format.latin_font = slides.FontData("Times New Roman")
second_paragraph.end_paragraph_portion_format = end_paragraph_format
text_frame.paragraphs.add(first_paragraph)
text_frame.paragraphs.add(second_paragraph)
presentation.save("end_paragraph_format.pptx", slides.export.SaveFormat.PPTX)
统计渲染行数
有关影响自动换行和行尾标点的段落规则,请参阅 Control Line Breaking 与 Control Hanging Punctuation。
使用 Paragraph.get_lines_count 可统计段落在文本布局后的占用行数(包括自动换行)。这在检查演示文稿模板的文本长度和布局时非常有用。
段落是 TextFrame.paragraphs 中的一个项目,可能占用多行渲染。段落内的显式换行符会强制新行但不会创建新段落。自动换行根据可用宽度产生新行,而不在文本中插入显式换行符。因此,仅统计段落或换行符字符并不能得到渲染行数。
下面的示例创建一个文本形状,统计其行数,缩窄形状,然后用更短的字符串替换文本。启用了换行并禁用了自动适应,以便形状宽度控制换行,而不会自动缩小文本或调整形状大小。形状尺寸以磅为单位。最后,示例再添加一个段落并汇总整个文本框的行数。
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, 400, 200)
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
paragraph = text_frame.paragraphs[0]
paragraph.paragraph_format.default_portion_format.font_height = 20
paragraph.text = "This text demonstrates how automatic wrapping changes the number of rendered lines."
print(f"Original width: {paragraph.get_lines_count()}")
shape.width = 150
print(f"Narrower shape: {paragraph.get_lines_count()}")
paragraph.text = "Short text."
print(f"Shorter text: {paragraph.get_lines_count()}")
second_paragraph = slides.Paragraph()
second_paragraph.text = "Another paragraph."
second_paragraph.paragraph_format.default_portion_format.font_height = 20
text_frame.paragraphs.add(second_paragraph)
total_line_count = 0
for current_paragraph in text_frame.paragraphs:
total_line_count += current_paragraph.get_lines_count()
print(f"Total lines in the text frame: {total_line_count}")
使用这些文本和尺寸,缩窄形状会增加行数,而用短字符串替换文本会减少行数。确切的计数会因字体可用性与替代、字体大小、边距、缩进、换行和自动适应设置而异。检查模板时请使用目标环境的字体和布局设置。
仅凭行数并不能决定文本是否溢出容器。可用高度、行高、段落和行间距以及自动适应行为也同样重要;即使是一行文本,在禁用换行时也可能超过可用宽度。
导入和导出段落内容
将 HTML 文本导入段落
使用 ParagraphCollection.add_from_html 可将 HTML 标记转换为文本框中的段落和片段。
- 创建 Presentation 类的实例。
- 访问一张幻灯片并添加一个 AutoShape。
- 获取形状的 TextFrame 并清除默认段落。
- 读取源 HTML 文件。
- 将 HTML 字符串传递给 ParagraphCollection.add_from_html。
- 保存修改后的演示文稿。
此 Python 示例将 HTML 导入文本框:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape_width = presentation.slide_size.size.width - 20
shape_height = presentation.slide_size.size.height - 20
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 10, shape_width, shape_height)
shape.fill_format.fill_type = slides.FillType.NO_FILL
shape.text_frame.paragraphs.clear()
with open("file.html", "r", encoding="utf-8") as html_stream:
html = html_stream.read()
shape.text_frame.paragraphs.add_from_html(html)
presentation.save("html_text.pptx", slides.export.SaveFormat.PPTX)
将段落文本导出为 HTML
使用 ParagraphCollection.export_to_html 可将选定范围的段落导出为 HTML。
- 创建 Presentation 类的实例并加载所需的演示文稿。
- 访问幻灯片并找到包含文本的 AutoShape。
- 获取形状的 TextFrame。
- 调用 ParagraphCollection.export_to_html,传入起始段落索引和要导出的段落数。
- 将返回的 HTML 字符串写入文件。
此 Python 示例导出第一个文本形状中的所有段落:
import aspose.slides as slides
with slides.Presentation("ExportingHTMLText.pptx") as presentation:
shape = presentation.slides[0].shapes[0]
if isinstance(shape, slides.AutoShape) and shape.text_frame is not None:
paragraphs = shape.text_frame.paragraphs
html = paragraphs.export_to_html(0, paragraphs.count, None)
with open("paragraphs.html", "w", encoding="utf-8") as html_stream:
html_stream.write(html)
else:
print("The first shape is not a text shape.")
将段落渲染为图像
Paragraph 提供 get_image 方法,可直接渲染单个段落。该方法返回一个 IImage,您可以使用 IImage.save 将其保存为文件或流。无需渲染包含的形状或手动裁剪位图。
如果段落在其父集合中找不到、没有有效的渲染边界,或无法渲染,get_image 方法可能返回 None。在保存之前请检查返回值,并使用返回的图像作为上下文管理器以释放资源。
以默认比例渲染段落
假设我们有一个名为 sample.pptx 的演示文稿,包含一张幻灯片,第一形状是包含三个段落的文本框。

下面的示例在默认比例下渲染第二段落,并将返回的图像以 PNG 格式保存:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
shape = presentation.slides[0].shapes[0]
if isinstance(shape, slides.AutoShape) and shape.text_frame is not None and shape.text_frame.paragraphs.count > 1:
paragraph = shape.text_frame.paragraphs[1]
paragraph_image = paragraph.get_image()
if paragraph_image is not None:
with paragraph_image:
paragraph_image.save("paragraph.png", slides.ImageFormat.PNG)
else:
print("The paragraph could not be rendered.")
else:
print("The expected text shape or paragraph was not found.")
结果:

在表格单元格中渲染段落并进行缩放
向 get_image 传递水平和垂直缩放因子,可控制渲染段落的大小。下面的示例创建一个表格,在第一单元格中以两倍默认宽度和高度渲染段落,并将结果保存为 PNG 图像:
import aspose.slides as slides
scale_x = 2
scale_y = 2
with slides.Presentation() as presentation:
slide = presentation.slides[0]
table = slide.shapes.add_table(50, 50, [300], [80])
paragraph = table.rows[0][0].text_frame.paragraphs[0]
paragraph.text = "Text in a table cell"
paragraph_image = paragraph.get_image(scale_x, scale_y)
if paragraph_image is not None:
with paragraph_image:
paragraph_image.save("table_paragraph.png", slides.ImageFormat.PNG)
else:
print("The paragraph could not be rendered.")
缩放因子为 1 时保持该轴的默认像素大小。例如,水平和垂直因子均为 2 时,生成的图像宽高约为默认尺寸的两倍,像素数量约为四倍。较大的因子通常可产生更清晰的文本,适用于放大或高分辨率输出,但也会增加内存使用和文件大小。小于 1 的因子会生成更小且细节更少的图像。使用相等的因子可保持段落的宽高比;不同的水平和垂直因子会独立拉伸输出。
在需要包含形状填充、边框或其他视觉上下文的情况下,仍然可以使用 Shape.get_image。若仅需段落图像,请使用 Paragraph.get_image。
常见问题
是否可以完全禁用文本框内的换行?
可以。将 TextFrameFormat.wrap_text 设置为禁用换行,使行不会在文本框边缘换行。
如何获取特定段落在幻灯片上的精确边界?
使用 Paragraph.get_rect 可获取段落的边界矩形。Portion.get_rect 则提供单个片段的边界。
段落对齐(左、右、居中或两端对齐)在哪里控制?
ParagraphFormat.alignment 是段落级设置,适用于整个段落,不受各片段格式的影响。
要在每行内垂直对齐不同字体大小的片段,请参阅 Align Fonts Within a Line。
是否可以为段落的部分内容设置校对语言?
可以。为单个片段设置 PortionFormat.language_id,即可在同一段落中包含多种语言的文本。