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

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

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

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
a_squared = MathematicalText("a").setSuperscript("2")
b_squared = MathematicalText("b").setSuperscript("2")
equation = MathematicalText("c").setSuperscript("2").join("=").join(a_squared).join("+").join(b_squared)
math_paragraph.add(equation)
presentation.save("pythagorean-theorem.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
添加分数
使用 divide 创建分数。您可以通过 MathFractionTypes 选择分数样式。

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathFractionTypes, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
fraction = MathematicalText("1").divide("x", MathFractionTypes.Skewed)
math_block = MathBlock(fraction)
math_paragraph.add(math_block)
presentation.save("fraction.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
对于堆叠式分数,使用 MathFractionTypes.Bar:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathFractionTypes, MathematicalText
stacked_fraction = MathematicalText("x + 1").divide("y - 1", MathFractionTypes.Bar)
添加根号
使用 radical 创建平方根、立方根或其他根。当前元素成为底数,参数成为指数。

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
radical = MathematicalText("x").radical("n")
math_block = MathBlock(radical)
math_paragraph.add(math_block)
presentation.save("radical.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
显示或隐藏平方根的指数
平方根可以显示显式的 2,或使用常规的 √x 记法。通过 MathRadical 的 setHideDegree 方法控制显示形式。两种形式均保留指数 2 和被开方数 x,在可编辑公式中可见。

示例将同一平方根保存两次。visible-degree.pptx 中指数可见。hidden-degree.pptx 中指数隐藏。
import jpime
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
radical = MathematicalText("x").radical("2")
math_block = MathBlock(radical)
math_shape = slide.getShapes().addMathShape(20, 20, 200, 100)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
math_paragraph.add(math_block)
radical.setHideDegree(False)
presentation.save("visible-degree.pptx", SaveFormat.Pptx)
radical.setHideDegree(True)
presentation.save("hidden-degree.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
添加函数和极限
使用 asArgumentOfFunction 或 function 添加如 sin(x)、log(x) 或自定义函数名的函数。对于极限,将 lim 放入 MathLimit 或使用 setLowerLimit。

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
limit = MathematicalText("lim").setLowerLimit("x\u2192\u221E").function("x")
math_block = MathBlock(limit)
math_paragraph.add(math_block)
presentation.save("functions-and-limits.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
对于自定义函数名,将函数名设为当前元素并用其参数调用 function:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathematicalText
custom_function = MathematicalText("f").function("x + 1")
添加 N 元运算符和积分
使用 nary 添加求和、并集、交集等大型运算符。使用 integral 添加积分。两种方法均可设置上下限。

import jpype
import asposeslides
if not jpime.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathNaryOperatorTypes, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
a_power = MathematicalText("a").setSuperscript("n-k")
summation_base = MathematicalText("x").setSuperscript("k").join(a_power)
summation = summation_base.nary(MathNaryOperatorTypes.Summation, "k=0", "n")
math_block = MathBlock(summation)
math_paragraph.add(math_block)
presentation.save("nary-operators.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
N 元运算符用于带可选上下限的大型运算符。像 +、-、= 这类简单运算符通常使用 MathematicalText 添加并组合到表达式中。
对于积分,使用 integral:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathIntegralTypes, MathematicalText
differential = MathematicalText("dx").toBox()
integral_base = MathematicalText("x").join(differential)
integral = integral_base.integral(MathIntegralTypes.Simple, "0", "1")
添加矩阵
使用 MathMatrix 创建行列。矩阵默认不包括括号,若需要圆括号、方括号或大括号,请自行在矩阵外围添加。

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathMatrix, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
matrix = MathMatrix(2, 3)
cell_0_0 = MathematicalText("1")
matrix.set_Item(0, 0, cell_0_0)
cell_0_1 = MathematicalText("x")
matrix.set_Item(0, 1, cell_0_1)
cell_1_0 = MathematicalText("x")
matrix.set_Item(1, 0, cell_1_0)
cell_1_1 = MathematicalText("2")
matrix.set_Item(1, 1, cell_1_1)
cell_1_2 = MathematicalText("y")
matrix.set_Item(1, 2, cell_1_2)
math_block = MathBlock(matrix)
math_paragraph.add(math_block)
presentation.save("matrix.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
添加公式数组
当需要对齐公式或垂直堆叠表达式时,使用 toMathArray。

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 140)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
equation_array = MathematicalText("x").join("y").toMathArray()
math_block = MathBlock(equation_array)
math_paragraph.add(math_block)
presentation.save("equation-array.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
使数组与周围元素对齐
setBaseJustification 控制数组相对于相邻数学元素的垂直对齐方式。下面的示例比较了第一行、数组中心和最后一行的对齐方式。
以下示例创建了三个完全相同的数组,每个数组都有三行,并在两侧分别放置 A= 和 =B。所有数学文本使用 Cambria Math,字号 24 磅。唯一变化的是数组的对齐方式。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, FontData, MathArray, MathBlock, MathJustification, MathematicalText, MathVerticalAlignment, Presentation, SaveFormat, ShapeType, SlideSizeScaleType, TextAnchorType
from java.awt import Color
def create_text(value):
text = MathematicalText(value)
text.getFormat().setFontHeight(24)
text.getFormat().setLatinFont(FontData("Cambria Math"))
return text
presentation = Presentation()
try:
presentation.getSlideSize().setSize(780, 200, SlideSizeScaleType.DoNotScale)
slide = presentation.getSlides().get_Item(0)
alignments = (("Top", MathVerticalAlignment.Top), ("Center", MathVerticalAlignment.Center), ("Bottom", MathVerticalAlignment.Bottom))
for index, (name, alignment) in enumerate(alignments):
x = 20 + index * 260
label = slide.getShapes().addAutoShape(ShapeType.Rectangle, x, 10, 240, 30)
label.getFillFormat().setFillType(FillType.NoFill)
label.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
label.getTextFrame().setText(name)
label_format = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat()
label_format.setFontHeight(16)
label_format.getFillFormat().setFillType(FillType.Solid)
label_format.getFillFormat().getSolidFillColor().setColor(Color.BLACK)
math_shape = slide.getShapes().addMathShape(x, 60, 240, 130)
math_shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top)
paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0)
math_paragraph = paragraph.getPortions().get_Item(0).getMathParagraph()
math_paragraph.setJustification(MathJustification.LeftJustified)
equation_array = MathArray(create_text("x+y=3"))
equation_array.getArguments().add(create_text("x-y=1"))
equation_array.getArguments().add(create_text("x=2"))
equation_array.setBaseJustification(alignment)
equation = MathBlock(create_text("A=")).join(equation_array).join(create_text("=B"))
math_paragraph.add(equation)
presentation.save("equation-array-alignment.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
下图展示了在 PowerPoint 中保存的演示文稿。请注意每个数组旁边的 A= 与 =B 的位置。

配置行间距
setRowSpacingRule 选择数组内部行的间距规则。setRowSpacing 为显式间距规则提供数值,预设规则会忽略该数值。
对于精确间距,数值以磅为单位。对于比例间距,数值以半行计:示例中的 3 表示 1.5 行。指定显式值时需同时设置两项属性;仅更改数值不会自动选择间距规则。
下面的示例保存了五个演示文稿,公式、对齐和字体设置相同。前三个使用预设,后两个展示显式数值。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FontData, MathArray, MathBlock, MathJustification, MathematicalText, MathRowSpacingRule, MathVerticalAlignment, Presentation, SaveFormat
def create_text(value):
text = MathematicalText(value)
text.getFormat().setFontHeight(24)
text.getFormat().setLatinFont(FontData("Cambria Math"))
return text
settings = (("SingleLineGap", MathRowSpacingRule.SingleLineGap, 0), ("OneAndAHalfLineGap", MathRowSpacingRule.OneAndAHalfLineGap, 0), ("TwoLineGap", MathRowSpacingRule.TwoLineGap, 0), ("Exactly", MathRowSpacingRule.Exactly, 20), ("Multiple", MathRowSpacingRule.Multiple, 3))
for name, rule, spacing in settings:
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 600, 300)
paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0)
paragraph.getParagraphFormat().setSpaceBefore(0)
paragraph.getParagraphFormat().setSpaceAfter(0)
math_paragraph = paragraph.getPortions().get_Item(0).getMathParagraph()
math_paragraph.setJustification(MathJustification.LeftJustified)
equation_array = MathArray(create_text("x+y=3"))
equation_array.getArguments().add(create_text("x-y=1"))
equation_array.getArguments().add(create_text("x=2"))
equation_array.setBaseJustification(MathVerticalAlignment.Center)
equation_array.setRowSpacingRule(rule)
equation_array.setRowSpacing(spacing)
equation = MathBlock(create_text("A=")).join(equation_array).join(create_text("=B"))
math_paragraph.add(equation)
presentation.save(f"equation-array-{name}.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
数组行间距独立于段落间距。setSpaceBefore 与 setSpaceAfter 为包含段落的上下添加空白,而不是数组行之间的空白。同样,MathParagraph.setJustification 控制整个数学段落的水平位置,不会覆盖数组的垂直对齐设置。
添加三角函数
当参数为当前元素且函数名已知时,使用 asArgumentOfFunction。

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathFunctionsOfOneArgument, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
cosine = MathematicalText("2x").asArgumentOfFunction(MathFunctionsOfOneArgument.Cos)
math_block = MathBlock(cosine)
math_paragraph.add(math_block)
presentation.save("trigonometric-function.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
添加下标和上标
使用下标和上标帮助函数添加索引和幂。当索引需要显示在基线左侧时,使用 setSubSuperscriptOnTheLeft。

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
scripts = MathematicalText("Y").setSubSuperscriptOnTheLeft("1", "n")
math_block = MathBlock(scripts)
math_paragraph.add(math_block)
presentation.save("subscript-superscript.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
添加定界符
使用 enclose 将表达式放入定界符中。对于包含多个元素的定界符表达式,还可以设置分隔符字符。

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
delimiter = MathematicalText("x").join("y").join("z").enclose('<', '>')
delimiter.setSeparatorCharacter('|')
math_block = MathBlock(delimiter)
math_paragraph.add(math_block)
presentation.save("delimiters.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
定界符的大小与对齐
将 True 传递给 setGrowToMatchOperandHeight 可使括号随高大的运算对象伸展,传 False 则保持常规大小。使用 setDelimiterShape 并选择两种 MathDelimiterShape 值之一。Centered 将括号在数学轴上居中(该轴穿过等号的中间),Match 则与所包围的内容对齐。两种设置仅影响定界符本身,内部表达式保持不变。
示例将分数放在 x 的指数位置,使表达式在基线之上延伸得更高。四个公式使用相同的括号和字体设置。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathDelimiterShape, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
for row, grow in enumerate((True, False)):
for column, shape in enumerate((MathDelimiterShape.Centered, MathDelimiterShape.Match)):
fraction = MathematicalText("a + b").divide("c + d")
power = MathematicalText("x").setSuperscript(fraction)
delimiter = power.enclose("(", ")")
delimiter.setGrowToMatchOperandHeight(grow)
delimiter.setDelimiterShape(shape)
math_shape = slide.getShapes().addMathShape(20 + 330 * column, 20 + 120 * row, 300, 100)
portion = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
portion.getPortionFormat().setFontHeight(32)
equation = MathematicalText("y = ").join(delimiter)
portion.getMathParagraph().add(equation)
presentation.save("delimiter-sizing.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
在 PowerPoint 中比较各行可见效果。顶部行的括号随指数伸展,左右两种形状效果相同。底部行关闭伸展,左侧较小的括号(Centered)与等号对齐,而右侧(Match)则在表达式上方更高。

添加边框框
对分数调用 toBorderBox 可为整个表达式加框。返回的 MathBorderBox 默认显示四条边。使用 setHideTop、setHideBottom、setHideLeft 与 setHideRight 可单独隐藏边。
示例在一张幻灯片上放置相同分数的六个副本。为演示消除效果,隐藏四条边并将 True 传递给 setStrikethroughBottomLeftToTopRight 生成上升对角线 (↗),或传给 setStrikethroughTopLeftToBottomRight 生成下降对角线 (↘)。亦可使用 setStrikethroughHorizontal 或 setStrikethroughVertical 与任意组合的可见边一起使用。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat, SlideSizeScaleType
presentation = Presentation()
try:
presentation.getSlideSize().setSize(540, 220, SlideSizeScaleType.DoNotScale)
slide = presentation.getSlides().get_Item(0)
def add_fraction(x, y, hide_border):
fraction = MathematicalText("x+1").divide("y+2")
box = fraction.toBorderBox()
box.setHideTop(hide_border)
box.setHideBottom(hide_border)
box.setHideLeft(hide_border)
box.setHideRight(hide_border)
shape = slide.getShapes().addMathShape(x, y, 160, 90)
portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
portion.getPortionFormat().setFontHeight(28)
math_block = MathBlock(box)
portion.getMathParagraph().add(math_block)
return box
add_fraction(10, 10, False)
partial = add_fraction(190, 10, False)
partial.setHideTop(True)
partial.setHideRight(True)
add_fraction(370, 10, True).setStrikethroughBottomLeftToTopRight(True)
add_fraction(10, 120, True).setStrikethroughTopLeftToBottomRight(True)
add_fraction(190, 120, True).setStrikethroughHorizontal(True)
add_fraction(370, 120, False).setStrikethroughVertical(True)
presentation.save("border-box.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()

在此分数上,水平划线会与分数线重叠。垂直划线穿过分子和分母,而外围边框仍然可见。这些线装饰整个表达式。普通文本的删除线只作用于文本运行中的字符。重新打开 PowerPoint 后,仍可分别编辑分子和分母。
使用幻影元素控制可见性和布局
将表达式包裹在 MathPhantom 中,可独立控制其可见性以及在公式中占用的空间。MathPhantom 的方法如下:
| 方法 | 效果 |
|---|---|
| setShow | 传 False 隐藏表达式。除非下面的方法也设为 True,否则其尺寸仍会影响布局。 |
| setZeroWidth | 将表达式的水平宽度视为零,使相邻元素更紧凑。 |
| setZeroAsc | 将其上升高度(基线之上的部分)视为零,可减少上方预留空间。 |
| setZeroDesc | 将其下降深度(基线之下的部分)视为零,可让分数线或其他内容靠近相邻元素。 |
基线是普通数学字符所在的参考线。隐藏内容不会删除其尺寸,将尺寸设为零也不删除表达式。底层表达式仍可通过 getBase 访问。调用 setShow 并传 True 可再次显示,尺寸设置保持不变。
下面示例创建了五种并排的变体:可见分数、保留空间的隐藏分数,以及三个将某一维度设为零的隐藏分数。每种变体使用相同的外围表达式和 24 磅 Cambria Math。所有公式形状的顶部位置相同,以灰色参考线标记。将幻影放在外部分数的分子中,可更直观地看到垂直布局的变化。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, FontData, MathBlock, MathFraction, MathematicalText, MathPhantom, Presentation, SaveFormat, ShapeType, SlideSizeScaleType, TextAnchorType
from java.awt import Color
def create_text(value):
text = MathematicalText(value)
text.getFormat().setFontHeight(24)
text.getFormat().setLatinFont(FontData("Cambria Math"))
return text
presentation = Presentation()
try:
presentation.getSlideSize().setSize(1000, 180, SlideSizeScaleType.DoNotScale)
slide = presentation.getSlides().get_Item(0)
labels = ("Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc")
guide = slide.getShapes().addAutoShape(ShapeType.Line, 10, 60, 980, 0)
guide.getLineFormat().getFillFormat().setFillType(FillType.Solid)
guide.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.LIGHT_GRAY)
for index, name in enumerate(labels):
left = 10 + index * 200
label = slide.getShapes().addAutoShape(ShapeType.Rectangle, left, 10, 190, 30)
label.getFillFormat().setFillType(FillType.NoFill)
label.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
label.getTextFrame().setText(name)
label_format = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat()
label_format.setFontHeight(14)
label_format.getFillFormat().setFillType(FillType.Solid)
label_format.getFillFormat().getSolidFillColor().setColor(Color.BLACK)
fraction_numerator = create_text("a+b")
fraction_denominator = create_text("c+d")
fraction = MathFraction(fraction_numerator, fraction_denominator)
phantom = MathPhantom(fraction)
phantom.setShow(index == 0)
phantom.setZeroWidth(index == 2)
phantom.setZeroAsc(index == 3)
phantom.setZeroDesc(index == 4)
left_text = create_text("x+")
right_text = create_text("+y")
numerator = MathBlock(left_text).join(phantom).join(right_text)
denominator = create_text("z")
equation = MathFraction(numerator, denominator)
shape = slide.getShapes().addMathShape(left, 60, 190, 100)
shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top)
math_portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
math_paragraph = math_portion.getMathParagraph()
math_block = MathBlock(equation)
math_paragraph.add(math_block)
presentation.save("math-phantom.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()

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

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathTopBotPositions, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
grouped = MathematicalText("x + y").group('\u23DF', MathTopBotPositions.Bottom, MathTopBotPositions.Top).setLowerLimit("any text")
math_block = MathBlock(grouped)
math_paragraph.add(math_block)
presentation.save("grouped-terms.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
格式化数学元素
仅在有助于澄清公式时使用格式化帮助器。例如,使用 overbar 在数学元素上方添加横线。

import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
overbar = MathematicalText("ABC").overbar()
math_block = MathBlock(overbar)
math_paragraph.add(math_block)
presentation.save("overbar.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
在已有公式中格式化文本
要在现有公式的所有文本中应用格式,遍历每个数学块的元素树。[MathElementBase.getChildren](https://reference.aspose.com/slides/python-java/aspose.slides/mathelementbase/#getChildren) 仅返回直接子元素,需要递归调用以深入到分数、上下标、根号等嵌套结构。检查当前元素及其子元素,以确保在元素为 MathematicalText 时也能被处理。
本示例假设 input.pptx 包含类似 z = xᵢ / ∛y 的公式,其中分子为下标,分母为根号。仅处理块的顶层元素会遗漏分数内部的文本。示例查找幻灯片文本框中的数学段落,访问其块,通过 [MathPortion.getMathParagraph](https://reference.aspose.com/slides/python-java/aspose.slides/mathportion/#getMathParagraph) 获取,并使用 [setFontHeight](https://reference.aspose.com/slides/python-java/aspose.slides/baseportionformat/#setFontHeight) 将每个文本元素的字体大小设置为 28 磅,调用其 [getFormat](https://reference.aspose.com/slides/python-java/aspose.slides/mathematicaltext/#getFormat) 方法。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AutoShape, MathematicalText, MathPortion, Presentation, SaveFormat
def format_math_text(element):
if isinstance(element, MathematicalText):
element.getFormat().setFontHeight(28)
for child in element.getChildren():
format_math_text(child)
presentation = Presentation("input.pptx")
try:
for slide in presentation.getSlides():
for shape in slide.getShapes():
if not isinstance(shape, AutoShape):
continue
if shape.getTextFrame() is None:
continue
for paragraph in shape.getTextFrame().getParagraphs():
for portion in paragraph.getPortions():
if isinstance(portion, MathPortion):
for block in portion.getMathParagraph():
format_math_text(block)
presentation.save("formatted-equations.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
保存的演示文稿在每个处理过的公式的每一层都更新了文本格式,同时保留了原有的文本内容和数学结构。
快速参考
| 任务 | 主要 API |
|---|---|
| 创建数学文本 | MathematicalText |
| 合并元素 | MathElementBase.join |
| 创建分数 | MathElementBase.divide |
| 添加上标或下标 | setSuperscript, setSubscript |
| 添加函数 | function, asArgumentOfFunction |
| 添加根号 | MathElementBase.radical |
| 添加极限 | setLowerLimit, setUpperLimit |
| 添加左侧上下标 | setSubSuperscriptOnTheLeft |
| 添加求和与积分 | nary, integral |
| 添加矩阵 | MathMatrix |
| 添加公式数组 | toMathArray |
| 添加定界符 | enclose |
| 添加横线与边框 | overbar, toBorderBox |
| 分组项 | group |
常见问答
我可以编辑已有的 PowerPoint 公式吗?
可以。打开演示文稿,找到包含 MathPortion 的形状,调用 getMathParagraph,并更新该段落中的数学块。
公式是否以可编辑的 PowerPoint 数学形式保存?
是的。保存为 PPTX 时,Aspose.Slides 会将公式写入可编辑的 Office 数学内容。
我能将公式导出为 LaTeX 吗?
可以。从其 MathPortion 获取 MathParagraph,调用 MathParagraph.toLatex 直接导出。完整示例请参阅 Export Math Equations from Presentations in Python。