在 Python 中从演示文稿导出数学公式

介绍

Aspose.Slides 允许您从演示文稿中导出数学公式。例如,您可能需要提取幻灯片(特定演示文稿)上的数学公式,并在其他程序或平台中使用它们。

将数学公式导出为 LaTeX

Aspose.Slides 可以直接将 PowerPoint 数学公式转换为 LaTeX;无需中间的 MathML 文件或外部转换器。数学公式存储在文本框中,形式为 MathPortion。使用 MathPortion.getMathParagraph 获取 MathParagraph,然后调用 MathParagraph.toLatex。该方法返回一个字符串,您可以保存、显示、发送到其他应用程序或进一步处理。

以下示例遍历每个幻灯片上的每个文本框,查找所有数学部分,并将每个公式写入单独的 .tex 文件:

from pathlib import Path

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathPortion, Presentation, SlideUtil

presentation = Presentation("equations.pptx")
try:
    slide_count = presentation.getSlides().size()
    for slide_index in range(slide_count):
        slide = presentation.getSlides().get_Item(slide_index)
        slide_number = slide_index + 1
        equation_number = 1
        text_frames = SlideUtil.getAllTextBoxes(slide)

        for text_frame in text_frames:
            for paragraph in text_frame.getParagraphs():
                for portion in paragraph.getPortions():
                    if not isinstance(portion, MathPortion):
                        continue

                    math_paragraph = portion.getMathParagraph()
                    latex_file_name = f"slide_{slide_number}_equation_{equation_number}.tex"
                    latex_text = math_paragraph.toLatex()
                    latex_path = Path(latex_file_name)
                    latex_path.write_text(str(latex_text), encoding="utf-8")
                    equation_number += 1
finally:
    presentation.dispose()

SlideUtil.getAllTextBoxes 返回在幻灯片上找到的所有文本框。对 MathPortion 进行类型检查,可将真正可编辑的公式与普通文本和图像区分开来。

LaTeX 引擎和文档模板并不都支持相同的命令、包或 Unicode 字符。请使用您的应用程序所使用的 LaTeX 引擎测试返回的字符串。如果某个符号或 Office Math 元素在该环境中没有合适的表示,请在返回的字符串中用项目特定的命令替换,或跳过该公式并记录问题以供审查。

将数学公式保存为 MathML

虽然人们可以轻松编写某些公式格式的代码,例如 LaTeX,但 MathML 因设计为自动生成而不易手写。由于 MathML 基于 XML,程序可以轻松读取和解析,因此在许多领域中常用作输出和打印格式。

以下示例代码展示了如何将演示文稿中的数学公式导出为 MathML:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathematicalText, Presentation
from java.io import FileOutputStream

presentation = Presentation()
try:
    math_shape = presentation.getSlides().get_Item(0).getShapes().addMathShape(0, 0, 500, 50)
    math_portion = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
    math_paragraph = math_portion.getMathParagraph()

    a_squared = MathematicalText("a").setSuperscript("2")
    b_squared = MathematicalText("b").setSuperscript("2")
    c_squared = MathematicalText("c").setSuperscript("2")
    equation = a_squared.join("+").join(b_squared).join("=").join(c_squared)
    math_paragraph.add(equation)

    stream = FileOutputStream("mathml.xml")
    try:
        math_paragraph.writeAsMathMl(stream)
    finally:
        stream.close()
finally:
    presentation.dispose()

常见问题

到底是导出 MathML 的段落还是单个公式块?

您可以导出整个数学段落(MathParagraph)或单个块(MathBlock)为 MathML。两种类型都提供写入 MathML 的方法。

如何判断幻灯片上的对象是数学公式而不是普通文本或图像?

公式存在于 MathPortion 中,并具有 MathParagraph。没有 MathParagraph 的图像和普通文本部分不是可导出的公式。

演示文稿中的 MathML 来自何处——是 PowerPoint 特有的还是标准?

导出目标是标准 MathML(XML)。Aspose 使用 Presentation MathML——标准的呈现子集,已在各种应用程序和网页上广泛使用。

是否支持导出表格、SmartArt、组合等内部的公式?

支持。如果这些对象包含具有 MathParagraph 的文本部分(即真正的 PowerPoint 公式),它们会被导出。如果公式以图像形式嵌入,则不会导出。

导出为 MathML 会修改原始演示文稿吗?

不会。写入 MathML 只是对公式内容的序列化,不会修改演示文稿文件。