Добавить математические уравнения в презентации PowerPoint на Python

Обзор

PowerPoint хранит уравнения в виде Office Math Markup Language (OMML). С помощью Aspose.Slides for Python via .NET вы можете программно создавать такой же математический контент: дроби, радикалы, функции, пределы, N‑арные операторы, матрицы, массивы и отформатированные блоки формул.

В PowerPoint пользователи обычно добавляют уравнения через Insert > Equation:

Вкладка Insert в PowerPoint с выбранной командой Equation

Результат — редактируемый математический текст на слайде:

Слайд PowerPoint, содержащий редактируемое математическое уравнение

Aspose.Slides формирует этот математический текст с помощью трёх основных объектов:

  • Math shape, создаваемый с помощью add_math_shape, является фигурой, содержащей уравнение.
  • MathPortion хранит математическое содержимое внутри текстового кадра фигуры.
  • MathParagraph содержит один или несколько объектов MathBlock.

Большинство примеров ниже используют MathematicalText и цепочечные методы из IMathElement для краткости и читаемости кода.

Для сценариев экспорта в MathML см. Экспорт уравнений из презентаций в Python через .NET.

Создать уравнение

Этот пример создаёт математическую фигуру и добавляет теорему Пифагора:

Уравнение c² = a² + b²

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)

Добавить дроби

Используйте divide для создания дроби. Вы можете выбрать стиль дроби с помощью MathFractionTypes.

Наклонённая математическая дробь, показывающая 1, разделённое на x

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)

Для сложенной (stacked) дроби используйте MathFractionTypes.BAR:

stacked_fraction = math.MathematicalText("x + 1").divide("y - 1", math.MathFractionTypes.BAR)

Добавить радикалы

Используйте radical для создания квадратного корня, кубического корня или другого корня. Текущий элемент становится основанием, а аргумент — степенью.

n‑й корень с x под радикалом

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) или пользовательских имён функций. Для пределов поместите lim в MathLimit или используйте set_lower_limit.

Предел x при x → ∞

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 когда нужны выровненные уравнения или вертикальная стопка выражений.

Вертикальный массив с x над y

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 когда аргумент — текущий элемент, а имя функции известно.

Тригонометрическая функция cos, применённая к 2x

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.

Заглавная Y с левым нижним индексом 1 и верхним индексом n

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 для помещения выражения в разделители. Можно также задать символ‑разделитель для выражений, содержащих несколько элементов.

Выражение с разделителями, содержащие x, y и z, разделённые вертикальными чертами

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 когда уравнение должно быть обрамлено.

Уравнение в рамке: a² = b² + c²

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 для размещения символа группировки над или под выражением. Добавьте предел, чтобы подписать сгруппированные термы.

Выражение x + y, сгруппированное с подписью любой текст снизу

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 помещает черту над математическим элементом.

Математическое выражение ABC с надстрочной чертой

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

FAQ

Могу ли я отредактировать существующее уравнение PowerPoint?

Да. Откройте презентацию, найдите фигуру, содержащую MathPortion, получите её MathParagraph и обновите математические блоки в этом абзаце.

Сохраняются ли уравнения как редактируемая математика PowerPoint?

Да. При сохранении в PPTX Aspose.Slides записывает уравнение как редактируемое содержимое Office Math.

Могу ли я экспортировать уравнения в LaTeX?

Aspose.Slides экспортирует математические уравнения в MathML. Если нужен LaTeX, сначала экспортируйте в MathML, а затем преобразуйте MathML с помощью инструмента, поддерживающего нужный диалект LaTeX.