在 Python 中向 PowerPoint 演示文稿添加数学公式
概述
PowerPoint 将公式存储为 Office Math Markup Language (OMML)。使用 Aspose.Slides for Python via .NET,您可以以编程方式创建相同类型的数学内容:分数、根式、函数、极限、N 元运算符、矩阵、数组以及格式化的数学块。
在 PowerPoint 中,用户通常通过 Insert > Equation 添加公式:

结果是幻灯片上的可编辑数学文本:

Aspose.Slides 通过以下三个主要对象构建该数学文本:
- 一个数学形状,由 add_math_shape 创建,用于容纳公式的形状。
- MathPortion 在形状的文本框中存储数学内容。
- MathParagraph 包含一个或多个 MathBlock 对象。
下面的大多数示例使用 MathematicalText 和来自 MathElementBase 的流式方法,以保持代码简洁易读。
有关 MathML 导出场景,请参阅 从演示文稿中导出数学公式(Python via .NET)。
创建公式
此示例使用 add_math_shape 创建数学形状并添加勾股定理:

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
equation = (
math.MathematicalText("c")
.set_superscript("2")
.join("=")
.join(math.MathematicalText("a").set_superscript("2"))
.join("+")
.join(math.MathematicalText("b").set_superscript("2"))
)
math_paragraph.add(equation)
presentation.save("pythagorean-theorem.pptx", slides.export.SaveFormat.PPTX)
组合数学块
当公式的部分独立构建为 MathBlock 对象时,使用 join_block 将它们组合。示例在左块中构建 c² =,在右块中构建 a² + b²。set_superscript 为这三个幂创建可编辑的数学上标。
import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_math_shape(20, 20, 600, 100)
math_paragraph = shape.text_frame.paragraphs[0].portions[0].math_paragraph
left_block = math.MathBlock()
left_block.add(math.MathematicalText("c").set_superscript("2"))
left_block.add(math.MathematicalText("="))
right_block = math.MathBlock()
right_block.add(math.MathematicalText("a").set_superscript("2"))
right_block.add(math.MathematicalText("+"))
right_block.add(math.MathematicalText("b").set_superscript("2"))
combined_block = left_block.join_block(right_block)
math_paragraph.add(combined_block)
presentation.save("equation.pptx", slides.export.SaveFormat.PPTX)
join_block 返回自身的 left_block,现在按顺序包含两个部分,同时将 right_block 置为空。仅一次将组合后的块添加到 MathParagraph。重新打开 equation.pptx 后,段落仅包含一个块,表示 c² = a² + b²,三个幂均保留为数学上标。
调用 add 如 left_block.add(right_block) 会尝试在另一个块内部直接嵌套块,并抛出包装了 .NET InvalidOperationException 的 RuntimeError。
添加分数
使用 divide 创建分数。可以通过 MathFractionTypes 选择分数样式。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
fraction = math.MathematicalText("1").divide("x", math.MathFractionTypes.SKEWED)
math_paragraph.add(math.MathBlock(fraction))
presentation.save("fraction.pptx", slides.export.SaveFormat.PPTX)
对于堆叠式分数,使用 MathFractionTypes.BAR:
import aspose.slides.mathtext as math
stacked_fraction = math.MathematicalText("x + 1").divide("y - 1", math.MathFractionTypes.BAR)
添加根式
使用 radical 创建平方根、立方根或其他根式。当前元素成为底数,参数成为指数。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
radical = math.MathematicalText("x").radical("n")
math_paragraph.add(math.MathBlock(radical))
presentation.save("radical.pptx", slides.export.SaveFormat.PPTX)
显示或隐藏平方根的指数
平方根可以显示显式的 2,或使用传统的 √x 表记。MathRadical 的 hide_degree 属性决定显示哪种形式。两种形式都保留指数 2 和被开方数 x。

示例将相同的平方根保存两次。visible-degree.pptx 中指数可见,hidden-degree.pptx 中指数隐藏。
import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
radical = math.MathematicalText("x").radical("2")
shape = slide.shapes.add_math_shape(20, 20, 200, 100)
portion = shape.text_frame.paragraphs[0].portions[0]
portion.math_paragraph.add(math.MathBlock(radical))
radical.hide_degree = False
presentation.save("visible-degree.pptx", slides.export.SaveFormat.PPTX)
radical.hide_degree = True
presentation.save("hidden-degree.pptx", slides.export.SaveFormat.PPTX)
添加函数和极限
使用 as_argument_of_function 或 function 添加 sin(x)、log(x) 等函数或自定义函数名。对于极限,将 lim 放入 MathLimit 或使用 set_lower_limit。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
limit = (
math.MathematicalText("lim")
.set_lower_limit("x\u2192\u221E")
.function("x")
)
math_paragraph.add(math.MathBlock(limit))
presentation.save("functions-and-limits.pptx", slides.export.SaveFormat.PPTX)
对于自定义函数名,将函数名设为当前元素并调用 function 并传入其参数:
import aspose.slides.mathtext as math
custom_function = math.MathematicalText("f").function("x + 1")
添加 N 元运算符和积分
使用 nary 添加求和、并集、交集等大型运算符。使用 integral 添加积分。两种方法都可以设置上下限。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
summation_base = (
math.MathematicalText("x")
.set_superscript("k")
.join(math.MathematicalText("a").set_superscript("n-k"))
)
summation = summation_base.nary(math.MathNaryOperatorTypes.SUMMATION, "k=0", "n")
math_paragraph.add(math.MathBlock(summation))
presentation.save("nary-operators.pptx", slides.export.SaveFormat.PPTX)
N 元运算符用于带可选上下限的大型运算符。诸如 +、-、= 的简单运算符通常使用 MathematicalText 并加入表达式中。
对于积分,使用 integral:
import aspose.slides.mathtext as math
integral_base = math.MathematicalText("x").join(math.MathematicalText("dx").to_box())
integral = integral_base.integral(math.MathIntegralTypes.SIMPLE, "0", "1")
添加矩阵
使用 MathMatrix 创建行列。矩阵默认不带括号,如需括号、方括号或花括号,请自行在矩阵外围添加。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
matrix = math.MathMatrix(2, 3)
matrix[0, 0] = math.MathematicalText("1")
matrix[0, 1] = math.MathematicalText("x")
matrix[1, 0] = math.MathematicalText("x")
matrix[1, 1] = math.MathematicalText("2")
matrix[1, 2] = math.MathematicalText("y")
math_paragraph.add(math.MathBlock(matrix))
presentation.save("matrix.pptx", slides.export.SaveFormat.PPTX)
编辑已有矩阵
假设线性代数课程以 matrix.pptx 中的系数矩阵开始(由前面的示例保存)。下一练习保留第一条公式,使用两条新公式替换第二条,并修改变量集合。将现有矩阵作为 MathMatrix 访问,以保留仍然适用的系数。

行列索引从零开始,指代当时矩阵的状态。在索引 1 之前插入一行会将原始第二行移动到索引 2。随后在索引 2 之后插入会追加一行。删除索引 2 的行会移除原始第二条公式,并将追加的行上移一位。列的移动方式相同。
import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation("matrix.pptx") as presentation:
shape = presentation.slides[0].shapes[0]
portion = shape.text_frame.paragraphs[0].portions[0]
matrix = portion.math_paragraph[0][0]
matrix.insert_row_before(1)
matrix.insert_row_after(2)
matrix.delete_row(2)
matrix.insert_column_before(1)
matrix.insert_column_after(3)
matrix.delete_column(3)
matrix[1, 0] = math.MathematicalText("12")
matrix[1, 1] = math.MathematicalText("1")
matrix[2, 2] = math.MathematicalText("10")
matrix[2, 3] = math.MathematicalText("1")
for row in range(matrix.row_count):
for column in range(matrix.column_count):
if matrix[row, column] is None:
matrix[row, column] = math.MathematicalText("0")
matrix.set_column_alignment(0, math.MathHorizontalAlignment.LEFT)
matrix.set_columns_alignment(1, 3, math.MathHorizontalAlignment.RIGHT)
presentation.save("updated-matrix.pptx", slides.export.SaveFormat.PPTX)
set_column_alignment 将第一列左对齐。set_columns_alignment 将从索引 1 开始的连续三列右对齐。在 updated-matrix.pptx 中,矩阵包含三条公式和四个变量。原始第一行的系数 1 和 x 现在出现在第一列和第三列。

添加公式数组
需要对齐公式或垂直堆叠表达式时,使用 to_math_array。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 140)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
equation_array = (
math.MathematicalText("x")
.join("y")
.to_math_array()
)
math_paragraph.add(math.MathBlock(equation_array))
presentation.save("equation-array.pptx", slides.export.SaveFormat.PPTX)
使数组与周围元素对齐
MathArray.base_justification 控制数组相对于相邻数学元素的垂直对齐方式。下例比较了第一行对齐、数组中心对齐和最后一行对齐的效果。
以下示例创建了三个相同的数组,每个数组有三行,左右相邻元素分别为 A= 和 =B。所有数学文本使用 Cambria Math,字号 24。仅改变数组的对齐方式。
import aspose.slides as slides
import aspose.slides.mathtext as math
def create_text(value):
text = math.MathematicalText(value)
text.format.font_height = 24
text.format.latin_font = slides.FontData("Cambria Math")
return text
with slides.Presentation() as presentation:
presentation.slide_size.set_size(780, 200, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
alignments = [math.MathVerticalAlignment.TOP, math.MathVerticalAlignment.CENTER, math.MathVerticalAlignment.BOTTOM]
labels = ["Top", "Center", "Bottom"]
for i, alignment in enumerate(alignments):
label = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20 + i * 260, 10, 240, 30)
label.fill_format.fill_type = slides.FillType.NO_FILL
label.line_format.fill_format.fill_type = slides.FillType.NO_FILL
label.text_frame.text = labels[i]
label_format = label.text_frame.paragraphs[0].portions[0].portion_format
label_format.font_height = 16
label_format.fill_format.fill_type = slides.FillType.SOLID
label_format.fill_format.solid_fill_color.color = slides.Color.black
math_shape = slide.shapes.add_math_shape(20 + i * 260, 60, 240, 130)
math_shape.text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.TOP
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
math_paragraph.justification = math.MathJustification.LEFT_JUSTIFIED
rows = [create_text("x+y=3"), create_text("x-y=1"), create_text("x=2")]
equation_array = math.MathArray(rows)
equation_array.base_justification = alignment
left_text = create_text("A=")
right_text = create_text("=B")
equation = math.MathBlock(left_text).join(equation_array).join(right_text)
math_paragraph.add(equation)
presentation.save("equation-array-alignment.pptx", slides.export.SaveFormat.PPTX)
下面的比较展示了在 PowerPoint 中保存的演示文稿。请注意每个数组旁边的 A= 和 =B 所在位置。

配置行间距
row_spacing_rule 选择数组内部行的间距规则。row_spacing 为显式间距规则提供数值,预设规则会忽略该属性。
显式间距的数值以点(pt)为单位。比例间距的数值以半行(half‑lines)计:示例中的 3 表示 1.5 行。指定显式值时需同时设置两个属性,仅更改数值不会自动选择间距规则。
下面的示例保存了五个演示文稿,使用相同的公式、对齐方式和字体设置。前三使用预设,后两使用显式数值。
import aspose.slides as slides
import aspose.slides.mathtext as math
def create_text(value):
text = math.MathematicalText(value)
text.format.font_height = 24
text.format.latin_font = slides.FontData("Cambria Math")
return text
settings = [
(math.MathRowSpacingRule.SINGLE_LINE_GAP, 0),
(math.MathRowSpacingRule.ONE_AND_A_HALF_LINE_GAP, 0),
(math.MathRowSpacingRule.TWO_LINE_GAP, 0),
(math.MathRowSpacingRule.EXACTLY, 20),
(math.MathRowSpacingRule.MULTIPLE, 3)
]
for rule, spacing in settings:
file_name = f"equation-array-{rule.name}.pptx"
with slides.Presentation() as presentation:
math_shape = presentation.slides[0].shapes.add_math_shape(20, 20, 600, 300)
paragraph = math_shape.text_frame.paragraphs[0]
paragraph.paragraph_format.space_before = 0
paragraph.paragraph_format.space_after = 0
math_paragraph = paragraph.portions[0].math_paragraph
math_paragraph.justification = math.MathJustification.LEFT_JUSTIFIED
rows = [create_text("x+y=3"), create_text("x-y=1"), create_text("x=2")]
equation_array = math.MathArray(rows)
equation_array.base_justification = math.MathVerticalAlignment.CENTER
equation_array.row_spacing_rule = rule
equation_array.row_spacing = spacing
left_text = create_text("A=")
right_text = create_text("=B")
equation = math.MathBlock(left_text).join(equation_array).join(right_text)
math_paragraph.add(equation)
presentation.save(file_name, slides.export.SaveFormat.PPTX)
数组行间距独立于段落间距。space_before 和 space_after 为包含段落的上下空间添加间距,而不是数组行之间的间距。同样,MathParagraph.justification 只控制整个数学段落的水平位置,不会替代数组的垂直对齐设置。
添加三角函数
当参数是当前元素且函数名称已知时,使用 as_argument_of_function。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
cosine = math.MathematicalText("2x").as_argument_of_function(
math.MathFunctionsOfOneArgument.COS
)
math_paragraph.add(math.MathBlock(cosine))
presentation.save("trigonometric-function.pptx", slides.export.SaveFormat.PPTX)
添加下标和上标
使用下标和上标助手处理索引和幂。当索引需出现在基底左侧时,使用 set_sub_superscript_on_the_left。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
scripts = math.MathematicalText("Y").set_sub_superscript_on_the_left("1", "n")
math_paragraph.add(math.MathBlock(scripts))
presentation.save("subscript-superscript.pptx", slides.export.SaveFormat.PPTX)
添加分界符
使用 enclose 将表达式放入分界符中。对于包含多个元素的分界符表达式,还可以设置分隔符字符。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
delimiter = (
math.MathematicalText("x")
.join("y")
.join("z")
.enclose("<", ">")
)
delimiter.separator_character = "|"
math_paragraph.add(math.MathBlock(delimiter))
presentation.save("delimiters.pptx", slides.export.SaveFormat.PPTX)
分界符的大小和对齐
将 grow_to_match_operand_height 设为 True,使括号随高操作数而伸长;设为 False 则保持普通大小。delimiter_shape 取值为两种 MathDelimiterShape 之一。CENTERED 将括号在数学轴(等号中线)上居中,MATCH 则与被包围内容对齐。两种设置仅影响分界符本身,内部表达式保持不变。
示例将分数放在 x 的指数位置,使表达式在基线上方延伸得更高。四个公式使用相同的括号和字体设置。
import aspose.slides as slides
import aspose.slides.mathtext as math
def add_expression(slide, left, top, grow, shape):
fraction = math.MathematicalText("a + b").divide("c + d")
power = math.MathematicalText("x").set_superscript(fraction)
delimiter = power.enclose("(", ")")
delimiter.grow_to_match_operand_height = grow
delimiter.delimiter_shape = shape
math_shape = slide.shapes.add_math_shape(left, top, 300, 100)
portion = math_shape.text_frame.paragraphs[0].portions[0]
portion.portion_format.font_height = 32
equation = math.MathematicalText("y = ").join(delimiter)
portion.math_paragraph.add(equation)
with slides.Presentation() as presentation:
slide = presentation.slides[0]
add_expression(slide, 20, 20, True, math.MathDelimiterShape.CENTERED)
add_expression(slide, 350, 20, True, math.MathDelimiterShape.MATCH)
add_expression(slide, 20, 140, False, math.MathDelimiterShape.CENTERED)
add_expression(slide, 350, 140, False, math.MathDelimiterShape.MATCH)
presentation.save("delimiter-sizing.pptx", slides.export.SaveFormat.PPTX)
在 PowerPoint 中比较各行可见效果。顶部行的括号随指数伸长且两种形状看起来相同;底部行关闭伸长,左侧 CENTERED 的小括号与等号对齐,右侧 MATCH 的小括号则更高地环绕表达式。

添加边框框
对分数调用 to_border_box 可为整个表达式添加框线。返回的 MathBorderBox 默认显示四条边。使用 hide_top、hide_bottom、hide_left、hide_right 可单独隐藏边线。
示例在一张幻灯片上放置相同分数的六个副本。为展示消除效果,隐藏四条边并设置 strikethrough_bottom_left_to_top_right 形成上升对角线 (↗) 或 strikethrough_top_left_to_bottom_right 形成下降对角线 (↘)。也可以使用 strikethrough_horizontal 或 strikethrough_vertical 并任意组合可见边线。
import aspose.slides as slides
import aspose.slides.mathtext as math
def add_fraction(slide, x, y, hide_border=False):
fraction = math.MathematicalText("x+1").divide("y+2")
box = fraction.to_border_box()
box.hide_top = box.hide_bottom = box.hide_left = box.hide_right = hide_border
shape = slide.shapes.add_math_shape(x, y, 160, 90)
portion = shape.text_frame.paragraphs[0].portions[0]
portion.portion_format.font_height = 28
portion.math_paragraph.add(math.MathBlock(box))
return box
with slides.Presentation() as presentation:
presentation.slide_size.set_size(540, 220, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
add_fraction(slide, 10, 10)
partial = add_fraction(slide, 190, 10)
partial.hide_top = True
partial.hide_right = True
add_fraction(slide, 370, 10, hide_border=True).strikethrough_bottom_left_to_top_right = True
add_fraction(slide, 10, 120, hide_border=True).strikethrough_top_left_to_bottom_right = True
add_fraction(slide, 190, 120, hide_border=True).strikethrough_horizontal = True
add_fraction(slide, 370, 120).strikethrough_vertical = True
presentation.save("border-box.pptx", slides.export.SaveFormat.PPTX)

在此分数上,水平划线与分数线重叠。垂直划线穿过分子和分母,而外围边框仍然可见。这些线装饰整个表达式。普通文本的删除线只作用于字符。当在 PowerPoint 中重新打开演示文稿时,仍可独立编辑分子和分母。
使用幻影元素控制可见性和布局
将表达式包裹在 MathPhantom 中,可独立控制其可见性以及对公式布局贡献的空间。其属性控制如下:
| 属性 | 效果 |
|---|---|
| show | 设为 False 可隐藏表达式。除非下面的对应属性为 True,否则其尺寸仍会参与布局。 |
| zero_width | 将表达式的水平宽度视为零,使相邻元素可更靠近。 |
| zero_asc | 将其上升高度(基线以上的高度)视为零,可减少基线以上保留的空间。 |
| zero_desc | 将其下降深度(基线以下的深度)视为零,可使分数线或其他内容更靠近其下方的邻近元素。 |
基线是普通数学字母所站立的参考线。隐藏内容不会移除其尺寸,将尺寸视为零也不会删除表达式。底层表达式仍可通过 base 访问。将 show 重新设为 True 可再次显示,尺寸设置保持不变。
下面的示例创建了五种并排变体:一个可见分数、一个保留空间的隐藏分数,以及三个分别将宽度、上升或下降设为零的隐藏分数。每种变体均使用相同的外围表达式和 Cambria Math,字号 24。所有公式形状的顶部位置相同,以灰色参考线标记。将幻影放在外部分数的分子中,可更直观地看到垂直布局的变化。
import aspose.slides as slides
import aspose.slides.mathtext as math
def create_text(value):
text = math.MathematicalText(value)
text.format.font_height = 24
text.format.latin_font = slides.FontData("Cambria Math")
return text
with slides.Presentation() as presentation:
presentation.slide_size.set_size(1000, 180, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
labels = ["Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc"]
guide = slide.shapes.add_auto_shape(slides.ShapeType.LINE, 10, 60, 980, 0)
guide.line_format.fill_format.fill_type = slides.FillType.SOLID
guide.line_format.fill_format.solid_fill_color.color = slides.Color.light_gray
for i, label_text in enumerate(labels):
left = 10 + i * 200
label = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, left, 10, 190, 30)
label.fill_format.fill_type = slides.FillType.NO_FILL
label.line_format.fill_format.fill_type = slides.FillType.NO_FILL
label.text_frame.text = label_text
label_format = label.text_frame.paragraphs[0].portions[0].portion_format
label_format.font_height = 14
label_format.fill_format.fill_type = slides.FillType.SOLID
label_format.fill_format.solid_fill_color.color = slides.Color.black
fraction_numerator = create_text("a+b")
fraction_denominator = create_text("c+d")
fraction = math.MathFraction(fraction_numerator, fraction_denominator)
phantom = math.MathPhantom(fraction)
phantom.show = i == 0
phantom.zero_width = i == 2
phantom.zero_asc = i == 3
phantom.zero_desc = i == 4
left_text = create_text("x+")
right_text = create_text("+y")
numerator = math.MathBlock(left_text).join(phantom).join(right_text)
denominator = create_text("z")
equation = math.MathFraction(numerator, denominator)
shape = slide.shapes.add_math_shape(left, 60, 190, 100)
shape.text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.TOP
math_paragraph = shape.text_frame.paragraphs[0].portions[0].math_paragraph
math_paragraph.add(math.MathBlock(equation))
presentation.save("math-phantom.pptx", slides.export.SaveFormat.PPTX)

分组项
使用 group 在表达式上方或下方放置分组字符。可为分组添加极限标签。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
grouped = (
math.MathematicalText("x + y")
.group(chr(0x23DF), math.MathTopBotPositions.BOTTOM, math.MathTopBotPositions.TOP)
.set_lower_limit("any text")
)
math_paragraph.add(math.MathBlock(grouped))
presentation.save("grouped-terms.pptx", slides.export.SaveFormat.PPTX)
格式化数学元素
仅在能提升公式可读性时使用格式化助手。例如,overbar 在数学元素上方加一条横线。

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
overbar = math.MathematicalText("ABC").overbar()
math_paragraph.add(math.MathBlock(overbar))
presentation.save("overbar.pptx", slides.export.SaveFormat.PPTX)
格式化已有公式中的文本
要格式化已有公式中的全部文本,遍历每个数学块的元素树。MathElementBase.get_children 只返回直接子节点,需递归调用以到达分数、上下标、根式等嵌套结构中的文本。既要检查当前元素,也要检查其子元素,以便在当前元素是 MathematicalText 时也能处理。
本示例假设 input.pptx 中包含公式 z = xᵢ / ∛y,其中分数的分子带下标,分母带根式。仅处理块的顶层元素会漏掉分数内部的文本。示例在幻灯片文本框中查找数学段落,访问其块的 MathPortion.math_paragraph,并通过每个文本元素的 format 将 font_height 设置为 28 磅。
import aspose.slides as slides
import aspose.slides.mathtext as math
def format_math_text(element):
if isinstance(element, math.MathematicalText):
element.format.font_height = 28
for child in element.get_children():
format_math_text(child)
with slides.Presentation("input.pptx") as presentation:
for slide in presentation.slides:
for shape in slide.shapes:
if not isinstance(shape, slides.AutoShape) or shape.text_frame is None:
continue
for paragraph in shape.text_frame.paragraphs:
for portion in paragraph.portions:
if isinstance(portion, math.MathPortion):
for block in portion.math_paragraph:
format_math_text(block)
presentation.save("formatted-equations.pptx", slides.export.SaveFormat.PPTX)
保存的演示文稿在每个已处理公式的各级别上都更新了文本格式,同时保留了文本内容和数学结构。
快速参考
| 任务 | 主要 API |
|---|---|
| 创建数学文本 | MathematicalText |
| 组合元素 | MathElementBase.join |
| 创建分数 | MathElementBase.divide |
| 添加上标或下标 | set_superscript, set_subscript |
| 添加函数 | function, as_argument_of_function |
| 添加根式 | radical |
| 添加极限 | set_lower_limit, set_upper_limit |
| 添加左侧上下标 | set_sub_superscript_on_the_left |
| 添加求和和积分 | nary, integral |
| 添加矩阵 | MathMatrix |
| 添加公式数组 | to_math_array |
| 添加分界符 | enclose |
| 添加横线和边框 | overbar, to_border_box |
| 分组项 | group |
常见问题
我可以编辑已有的 PowerPoint 公式吗?
可以。打开演示文稿,找到包含 MathPortion 的形状,获取其 math_paragraph,并更新该段落中的数学块。
公式会以可编辑的 PowerPoint 数学形式保存吗?
会。保存为 PPTX 时,Aspose.Slides 会将公式写入可编辑的 Office 数学内容。
我可以将公式导出为 LaTeX 吗?
可以。获取公式的 MathParagraph(来自其 MathPortion),然后调用 MathParagraph.to_latex 直接导出。完整示例请参阅 Export Math Equations from Presentations in Python via .NET。