Dodaj równania matematyczne do prezentacji PowerPoint w Pythonie
Przegląd
PowerPoint przechowuje równania jako Office Math Markup Language (OMML). Z Aspose.Slides dla Pythona przez .NET możesz programowo tworzyć tego samego rodzaju treść matematyczną: ułamki, pierwiastki, funkcje, granice, operatory N‑argumentowe, macierze, tablice i sformatowane bloki matematyczne.
W PowerPoint użytkownicy zazwyczaj dodają równania z Wstaw > Równanie:

Wynikiem jest edytowalny tekst matematyczny na slajdzie:

Aspose.Slides buduje ten tekst matematyczny przy użyciu trzech głównych obiektów:
- Kształt matematyczny, utworzony przy pomocy add_math_shape, jest kształtem, który zawiera równanie.
- Klasa MathPortion przechowuje treść matematyczną wewnątrz ramki tekstowej kształtu.
- Klasa MathParagraph zawiera jeden lub więcej obiektów MathBlock.
Większość przykładów poniżej używa MathematicalText i płynnych metod z IMathElement, aby kod był krótki i czytelny.
W przypadkach eksportu MathML zobacz Export Math Equations from Presentations in Python via .NET.
Utwórz równanie
Ten przykład tworzy kształt matematyczny i dodaje twierdzenie Pitagorasa:

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 tworzy kształt, który już zawiera akapit matematyczny. Uzyskaj pierwszą MathPortion, pobierz jej MathParagraph i dodaj do niego bloki matematyczne lub elementy matematyczne.
Dodaj ułamki
Użyj divide aby utworzyć ułamek. Możesz wybrać styl ułamka przy pomocy 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)
Aby uzyskać ułamek w pionie, użyj MathFractionTypes.BAR:
stacked_fraction = math.MathematicalText("x + 1").divide("y - 1", math.MathFractionTypes.BAR)
Dodaj pierwiastki
Użyj radical aby utworzyć pierwiastek kwadratowy, sześcienny lub inny. Bieżący element staje się podstawą, a argument określa stopień pierwiastka.

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)
Dodaj funkcje i granice
Użyj as_argument_of_function lub function aby wstawić funkcje takie jak sin(x), log(x) lub nazwy funkcji niestandardowych. Dla granic umieść lim w MathLimit lub użyj 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)
Aby użyć niestandardowej nazwy funkcji, ustaw nazwę funkcji jako bieżący element:
custom_function = math.MathematicalText("f").function("x + 1")
Dodaj operatory N‑argumentowe i całki
Użyj nary aby dodać sumy, sumowania, przekroje i inne duże operatory. Użyj integral aby dodać całki. Obie metody pozwalają ustawić limity dolny i górny.

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)
Operatory N‑argumentowe służą do dużych operatorów z opcjonalnymi limitami. Proste operatory takie jak +, - i = zazwyczaj dodaje się jako MathematicalText i łączy w wyrażeniu.
Aby dodać całkę, użyj integral:
integral_base = math.MathematicalText("x").join(math.MathematicalText("dx").to_box())
integral = integral_base.integral(math.MathIntegralTypes.SIMPLE, "0", "1")
Dodaj macierze
Użyj MathMatrix aby określić wiersze i kolumny. Macierze domyślnie nie zawierają nawiasów, więc otocz macierz nawiasami, gdy potrzebne są nawiasy okrągłe, kwadratowe lub klamrowe.

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)
Dodaj tablice równań
Użyj to_math_array gdy potrzebujesz wyrównanych równań lub pionowego stosu wyrażeń.

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)
Dodaj funkcje trygonometryczne
Użyj as_argument_of_function gdy argument jest bieżącym elementem, a nazwa funkcji jest znana.

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)
Dodaj indeksy dolne i górne
Użyj pomocników indeksu dolnego i górnego dla indeksów i potęg. Gdy indeksy mają pojawić się po lewej stronie podstawy, użyj 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)
Dodaj delimitery
Użyj enclose aby umieścić wyrażenie wewnątrz delimiterów. Możesz także ustawić znak separatora dla wyrażeń delimiterów zawierających wiele elementów.

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)
Dodaj ramkę obramowania
Użyj to_border_box gdy równanie ma być otoczone ramką.

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)
Grupuj wyrazy
Użyj group aby umieścić znak grupowania nad lub pod wyrażeniem. Dodaj limit, aby oznaczyć pogrupowane wyrazy.

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)
Formatuj elementy matematyczne
Używaj pomocników formatowania tylko tam, gdzie wyjaśniają wzór. Na przykład, overbar umieszcza pasek nad elementem matematycznym.

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)
Szybkie odniesienie
| Zadanie | Główne API |
|---|---|
| Utwórz tekst matematyczny | MathematicalText |
| Połącz elementy | IMathElement.join |
| Utwórz ułamki | IMathElement.divide |
| Dodaj indeks górny lub dolny | set_superscript, set_subscript |
| Dodaj funkcje | function, as_argument_of_function |
| Dodaj pierwiastki | radical |
| Dodaj granice | set_lower_limit, set_upper_limit |
| Dodaj indeksy po lewej stronie | set_sub_superscript_on_the_left |
| Dodaj sumy i całki | nary, integral |
| Dodaj macierze | MathMatrix |
| Dodaj tablice równań | to_math_array |
| Dodaj delimitery | enclose |
| Dodaj paski i ramki | overbar, to_border_box |
| Grupuj wyrazy | group |
FAQ
Czy mogę edytować istniejące równanie w PowerPoint?
Tak. Otwórz prezentację, znajdź kształt zawierający MathPortion, pobierz jego MathParagraph i zaktualizuj bloki matematyczne w tym akapicie.
Czy równania są zapisywane jako edytowalna matematyka PowerPoint?
Tak. Po zapisaniu jako PPTX, Aspose.Slides zapisuje równanie jako edytowalną treść Office Math.
Czy mogę wyeksportować równania do LaTeX?
Aspose.Slides eksportuje równania matematyczne do MathML. Jeśli potrzebujesz LaTeX, najpierw wyeksportuj do MathML, a następnie skonwertuj MathML przy pomocy narzędzia obsługującego wybrany dialekt LaTeX.