Aggiungi equazioni matematiche alle presentazioni PowerPoint in Python

Panoramica

PowerPoint memorizza le equazioni come Office Math Markup Language (OMML). Con Aspose.Slides per Python via .NET, è possibile creare lo stesso tipo di contenuto matematico in modo programmatico: frazioni, radicali, funzioni, limiti, operatori N‑ari, matrici, array e blocchi matematici formattati.

In PowerPoint, gli utenti aggiungono normalmente le equazioni da Insert > Equation:

Scheda Inserisci di PowerPoint con il comando Equazione selezionato

Il risultato è testo matematico modificabile nella diapositiva:

Una diapositiva PowerPoint contenente un’equazione matematica modificabile

Aspose.Slides costruisce quel testo matematico attraverso tre oggetti principali:

  • Una forma matematica, creata con add_math_shape, è la forma che contiene l’equazione.
  • MathPortion memorizza il contenuto matematico all’interno del riquadro di testo della forma.
  • MathParagraph contiene uno o più oggetti MathBlock.

La maggior parte degli esempi seguenti utilizza MathematicalText e i metodi fluenti di IMathElement per mantenere il codice breve e leggibile.

Per scenari di esportazione MathML, vedere Esporta equazioni matematiche da presentazioni in Python via .NET.

Crea un’equazione

Questo esempio crea una forma matematica e aggiunge il teorema di Pitagora:

L’equazione c al quadrato uguale a a al quadrato più b al quadrato

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)

Aggiungi frazioni

Usa divide per creare una frazione. È possibile scegliere uno stile di frazione con MathFractionTypes.

Una frazione matematica inclinata che mostra 1 diviso 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)

Per una frazione impilata, usa MathFractionTypes.BAR:

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

Aggiungi radicali

Usa radical per creare una radice quadrata, cubica o altra radice. L’elemento corrente diventa la base e l’argomento diventa il grado.

Un’espressione radicale di n‑esima radice con x sotto il segno radicale

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)

Aggiungi funzioni e limiti

Usa as_argument_of_function o function per funzioni come sin(x), log(x) o nomi di funzioni personalizzati. Per i limiti, inserisci lim in un MathLimit o usa set_lower_limit.

Il limite di x quando x tende a infinito

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)

Per un nome di funzione personalizzato, rendi il nome della funzione l’elemento corrente:

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

Aggiungi operatori N‑ari e integrali

Usa nary per sommatorie, unioni, intersezioni e altri operatori di grandi dimensioni. Usa integral per gli integrali. Entrambi i metodi consentono di impostare i limiti inferiore e superiore.

Una sommatoria con limiti inferiore e superiore

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)

Gli operatori N‑ari sono per operatori grandi con limiti opzionali. Operatorii semplici come +, - e = sono solitamente aggiunti come MathematicalText e uniti all’espressione.

Per un integrale, usa integral:

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

Aggiungi matrici

Usa MathMatrix per righe e colonne. Le matrici non includono parentesi graffe per impostazione predefinita, quindi racchiudi la matrice quando ti servono parentesi tonde, quadre o graffe.

Una matrice matematica a due righe con una cella vuota

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)

Aggiungi array di equazioni

Usa to_math_array quando hai bisogno di equazioni allineate o di una pila verticale di espressioni.

Un array matematico verticale con x sopra 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)

Aggiungi funzioni trigonometriche

Usa as_argument_of_function quando l’argomento è l’elemento corrente e il nome della funzione è noto.

La funzione trigonometrica cos applicata a 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)

Aggiungi pedici e apici

Usa gli helper per pedice e apice per indici e potenze. Quando gli indici devono apparire sul lato sinistro della base, usa set_sub_superscript_on_the_left.

Una Y maiuscola con pedice sinistro 1 e apice 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)

Aggiungi delimitatori

Usa enclose per inserire un’espressione dentro delimitatori. Puoi anche impostare un carattere separatore per espressioni delimitate che contengono più elementi.

Un’espressione delimitata contenente x, y e z separati da barre verticali

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)

Aggiungi un riquadro bordato

Usa to_border_box quando l’equazione stessa deve essere incorniciata.

Un’equazione incorniciata che mostra a al quadrato uguale a b al quadrato più c al quadrato

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)

Raggruppa termini

Usa group per posizionare un carattere di raggruppamento sopra o sotto un’espressione. Aggiungi un limite per etichettare i termini raggruppati.

L’espressione x più y raggruppata con l’etichetta qualsiasi testo sotto di essa

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)

Formatta elementi matematici

Usa gli helper di formattazione solo dove chiariscono la formula. Ad esempio, overbar inserisce una barra sopra un elemento matematico.

Un’espressione matematica ABC con una barra sopra

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)

Riferimento rapido

Attività API principale
Crea testo matematico MathematicalText
Combina elementi IMathElement.join
Crea frazioni IMathElement.divide
Aggiungi apice o pedice set_superscript, set_subscript
Aggiungi funzioni function, as_argument_of_function
Aggiungi radicali radical
Aggiungi limiti set_lower_limit, set_upper_limit
Aggiungi script sul lato sinistro set_sub_superscript_on_the_left
Aggiungi sommatorie e integrali nary, integral
Aggiungi matrici MathMatrix
Aggiungi array di equazioni to_math_array
Aggiungi delimitatori enclose
Aggiungi barre e bordi overbar, to_border_box
Raggruppa termini group

FAQ

Posso modificare un’equazione PowerPoint esistente?

Sì. Apri la presentazione, trova la forma che contiene un MathPortion, ottieni il suo MathParagraph e aggiorna i blocchi matematici in quel paragrafo.

Le equazioni vengono salvate come math di PowerPoint modificabile?

Sì. Quando salvi in PPTX, Aspose.Slides scrive l’equazione come contenuto Office math modificabile.

Posso esportare le equazioni in LaTeX?

Sì. Ottieni il MathParagraph dell’equazione dal suo MathPortion, e chiama MathParagraph.to_latex per esportarlo direttamente. Per un esempio completo, vedere Esporta equazioni matematiche da presentazioni in Python via .NET.