Mathgleichungen zu PowerPoint-Präsentationen in Python hinzufügen

Überblick

PowerPoint speichert Gleichungen als Office Math Markup Language (OMML). Mit Aspose.Slides for Python via .NET können Sie dieselbe Art von mathematischem Inhalt programmgesteuert erstellen: Brüche, Wurzeln, Funktionen, Grenzen, N‑äre Operatoren, Matrizen, Arrays und formatierte Mathematikblöcke.

In PowerPoint fügen Benutzer Gleichungen normalerweise über Einfügen > Gleichung hinzu:

PowerPoint-Registerkarte Einfügen mit ausgewähltem Befehl Gleichung

Das Ergebnis ist editierbarer mathematischer Text auf der Folie:

Eine PowerPoint-Folie, die eine editierbare mathematische Gleichung enthält

Aspose.Slides erzeugt diesen mathematischen Text über drei Hauptobjekte:

Die meisten Beispiele unten verwenden MathematicalText und die Fluent‑Methoden von IMathElement, um den Code kurz und lesbar zu halten.

Für MathML‑Export‑Szenarien siehe Export Math Equations from Presentations in Python via .NET.

Gleichung erstellen

Dieses Beispiel erstellt ein Mathe‑Shape und fügt den Satz des Pythagoras hinzu:

Die Gleichung 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)

Brüche hinzufügen

Verwenden Sie divide, um einen Bruch zu erstellen. Sie können einen Bruchstil mit MathFractionTypes wählen.

Ein schräger mathematischer Bruch, der 1 durch x zeigt

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)

Für einen gestapelten Bruch verwenden Sie MathFractionTypes.BAR:

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

Wurzeln hinzufügen

Verwenden Sie radical, um eine Quadratwurzel, Kubikwurzel oder andere Wurzel zu erstellen. Das aktuelle Element wird zur Basis, das Argument zum Exponenten.

Ein n‑te Wurzel‑Ausdruck mit x unter dem Wurzelzeichen

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)

Funktionen und Grenzen hinzufügen

Verwenden Sie as_argument_of_function oder function für Funktionen wie sin(x), log(x) oder benutzerdefinierte Funktionsnamen. Für Grenzen setzen Sie lim in ein MathLimit oder verwenden set_lower_limit.

Der Limes von x, wenn x gegen unendlich geht

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)

Für einen benutzerdefinierten Funktionsnamen machen Sie den Funktionsnamen zum aktuellen Element:

custom_function = math.MathematicalText("f").function("x + 1")

N‑äre Operatoren und Integrale hinzufügen

Verwenden Sie nary für Summen, Vereinigungen, Schnittmengen und andere große Operatoren. Verwenden Sie integral für Integrale. Beide Methoden erlauben das Festlegen von Unter‑ und Obergrenzen.

Eine Summation mit Unter‑ und Obergrenzen

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‑äre Operatoren sind für große Operatoren mit optionalen Grenzen. Einfache Operatoren wie +, - und = werden normalerweise als MathematicalText hinzugefügt und zu dem Ausdruck verbunden.

Für ein Integral verwenden Sie integral:

integral_base = math.MathematicalText("x").join(math.MathematicalText("dx").to_box())
integral = integral_base.integral(math.MathIntegralTypes.SIMPLE, "0", "1")

Matrizen hinzufügen

Verwenden Sie MathMatrix für Zeilen und Spalten. Matrizen enthalten standardmäßig keine Klammern, daher schließen Sie die Matrix ein, wenn Sie Klammern, eckige Klammern oder geschweifte Klammern benötigen.

Eine zweizeilige mathematische Matrix mit einer leeren Zelle

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)

Gleichungsarrays hinzufügen

Verwenden Sie to_math_array, wenn Sie ausgerichtete Gleichungen oder einen vertikalen Stapel von Ausdrücken benötigen.

Ein vertikales mathematisches Array mit x über 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)

Trigonometrische Funktionen hinzufügen

Verwenden Sie as_argument_of_function, wenn das Argument das aktuelle Element ist und der Funktionsname bekannt ist.

Die trigonometrische Funktion cos angewendet auf 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)

Tief‑ und Hochstellungen hinzufügen

Verwenden Sie die Hilfsfunktionen für Tief‑ und Hochstellung für Indizes und Potenzen. Wenn die Indizes links von der Basis erscheinen sollen, verwenden Sie set_sub_superscript_on_the_left.

Ein großes Y mit linksseitigem Tiefstellungswert 1 und Hochstellungswert 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)

Trennzeichen hinzufügen

Verwenden Sie enclose, um einen Ausdruck in Trennzeichen zu setzen. Sie können auch ein Trennzeichen‑Zeichen für Ausdrücke mit mehreren Elementen festlegen.

Ein Trennzeichen‑Ausdruck, der x, y und z enthält und durch senkrechte Striche getrennt ist

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)

Rahmen‑Box hinzufügen

Verwenden Sie to_border_box, wenn die Gleichung selbst gerahmt werden soll.

Eine eingerahmte Gleichung, die a² = b² + c² zeigt

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)

Terme gruppieren

Verwenden Sie group, um ein Gruppierungszeichen über oder unter einem Ausdruck zu platzieren. Fügen Sie ein Limit hinzu, um die gruppierten Terme zu beschriften.

Der Ausdruck x + y, gruppiert mit der Beschriftung irgendein Text darunter

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)

Mathematische Elemente formatieren

Verwenden Sie Formatierungs‑Hilfsfunktionen nur dort, wo sie die Formel verdeutlichen. Zum Beispiel legt overbar einen Balken über ein Math‑Element.

Ein mathematischer Ausdruck ABC mit einem Überstrich

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)

Schnellreferenz

Aufgabe Haupt‑API
Mathe‑Text erstellen MathematicalText
Elemente kombinieren IMathElement.join
Brüche erstellen IMathElement.divide
Hoch‑ oder Tiefstellung hinzufügen set_superscript, set_subscript
Funktionen hinzufügen function, as_argument_of_function
Wurzeln hinzufügen radical
Grenzen hinzufügen set_lower_limit, set_upper_limit
Linksseitige Skripte hinzufügen set_sub_superscript_on_the_left
Summen und Integrale hinzufügen nary, integral
Matrizen hinzufügen MathMatrix
Gleichungsarrays hinzufügen to_math_array
Trennzeichen hinzufügen enclose
Balken und Rahmen hinzufügen overbar, to_border_box
Terme gruppieren group

FAQ

Kann ich eine vorhandene PowerPoint‑Gleichung bearbeiten?

Ja. Öffnen Sie die Präsentation, finden Sie das Shape, das einen MathPortion enthält, holen Sie dessen MathParagraph und aktualisieren Sie die Math‑Blöcke in diesem Paragraphen.

Werden Gleichungen als editierbare PowerPoint‑Mathematik gespeichert?

Ja. Beim Speichern als PPTX schreibt Aspose.Slides die Gleichung als editierbaren Office‑Mathe‑Inhalt.

Kann ich Gleichungen nach LaTeX exportieren?

Aspose.Slides exportiert mathematische Gleichungen nach MathML. Wenn Sie LaTeX benötigen, exportieren Sie zuerst nach MathML und konvertieren Sie dann das MathML mit einem Tool, das Ihren Ziel‑LaTeX‑Dialekt unterstützt.