Math Text
Contents
[
Hide
]
This article demonstrates working with mathematical text shapes and formatting equations using Aspose.Slides for Python via Java.
Install the package as described in Installation. Each example imports asposeslides before starting the JVM, then imports the API after the JVM is running.
Add Math Text
Create a math shape containing a fraction and the Pythagorean formula.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
# Add a math shape to the slide.
math_shape = slide.getShapes().addMathShape(0, 0, 720, 150)
# Access the math paragraph.
paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0)
text_portion = paragraph.getPortions().get_Item(0)
math_paragraph = text_portion.getMathParagraph()
# Add a simple fraction: x / y.
fraction = MathematicalText("x").divide("y")
math_paragraph.add(MathBlock(fraction))
# Add equation: c² = a² + b².
math_block = MathematicalText("c").setSuperscript("2").join("=").join(MathematicalText("a").setSuperscript("2")).join("+").join(MathematicalText("b").setSuperscript("2"))
math_paragraph.add(math_block)
finally:
presentation.dispose()
Access Math Text
Locate a shape that contains a math paragraph on the slide.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AutoShape, MathBlock, MathematicalText, MathPortion, Presentation
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
# Add a math shape that can be found below.
created_math_shape = slide.getShapes().addMathShape(50, 50, 100, 50)
created_paragraph = created_math_shape.getTextFrame().getParagraphs().get_Item(0)
created_portion = created_paragraph.getPortions().get_Item(0)
created_math_paragraph = created_portion.getMathParagraph()
created_fraction = MathematicalText("x").divide("y")
created_math_paragraph.add(MathBlock(created_fraction))
# Find the first shape that contains a math paragraph.
math_shape = None
for shape in slide.getShapes():
if isinstance(shape, AutoShape):
text_frame = shape.getTextFrame()
if text_frame is not None:
has_math = False
for paragraph in text_frame.getParagraphs():
for portion in paragraph.getPortions():
if isinstance(portion, MathPortion):
has_math = True
break
if has_math:
break
if has_math:
math_shape = shape
break
if math_shape is not None:
paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0)
text_portion = paragraph.getPortions().get_Item(0)
math_paragraph = text_portion.getMathParagraph()
# Example: create a fraction (not added here).
fraction = MathematicalText("x").divide("y")
# Use math_paragraph or fraction as needed.
finally:
presentation.dispose()
Remove Math Text
Delete a math shape from the slide.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(50, 50, 100, 50)
paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0)
text_portion = paragraph.getPortions().get_Item(0)
math_paragraph = text_portion.getMathParagraph()
fraction = MathematicalText("x").divide("y")
math_paragraph.add(MathBlock(fraction))
# Remove the math shape.
slide.getShapes().remove(math_shape)
finally:
presentation.dispose()
Format Math Text
Set font properties for a math portion.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(50, 50, 100, 50)
paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0)
text_portion = paragraph.getPortions().get_Item(0)
math_paragraph = text_portion.getMathParagraph()
fraction = MathematicalText("x").divide("y")
math_paragraph.add(MathBlock(fraction))
text_portion.getPortionFormat().setFontHeight(20)
finally:
presentation.dispose()