在 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 和来自 IMathElement 的流式方法,以保持代码简洁易读。
有关 MathML 导出场景,请参阅 Export Math Equations from Presentations in Python via .NET。
创建公式
此示例创建一个数学形状并添加勾股定理:

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)
add_math_shape 创建的形状已包含一个数学段落。访问第一个 MathPortion,获取其 MathParagraph,并向其中添加数学块或数学元素。
添加分数
使用 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:
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)
添加函数和极限
使用 as_argument_of_function 或 function 来表示 sin(x)、log(x) 或自定义函数名等函数。对于极限,可在 MathLimit 中放置 lim,或使用 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)
对于自定义函数名,只需将函数名设为当前元素:
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:
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)
添加公式数组
当需要对齐的公式或垂直堆叠的表达式时,使用 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)
添加三角函数
当函数名已知且参数为当前元素时,使用 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)
添加边框盒
使用 to_border_box 将整个公式框起来。

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
boxed_equation = (
math.MathematicalText("a")
.set_superscript("2")
.join("=")
.join(math.MathematicalText("b").set_superscript("2"))
.join("+")
.join(math.MathematicalText("c").set_superscript("2"))
.to_border_box()
)
math_paragraph.add(math.MathBlock(boxed_equation))
presentation.save("border-box.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)
快速参考
| 任务 | 主要 API |
|---|---|
| 创建数学文本 | MathematicalText |
| 合并元素 | IMathElement.join |
| 创建分数 | IMathElement.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 的形状,获取其 MathParagraph,并在该段落中更新数学块。
公式会以可编辑的 PowerPoint 数学形式保存吗?
会。保存为 PPTX 时,Aspose.Slides 会将公式写入可编辑的 Office 数学内容。
我能将公式导出为 LaTeX 吗?
Aspose.Slides 将公式导出为 MathML。如果需要 LaTeX,请先导出为 MathML,然后使用支持目标 LaTeX 方言的工具将 MathML 转换为 LaTeX。