How to Use Aspose.Slides for Python to Extract Math Equations from a Presentation?

Hi. i have a question, How can I replace the math equations in paragraphs, and convert them to latex and then export the ppt file to html data?

@ZZZ21321,
Thank you for contacting support.

I am working on the question and will get back to you as soon as possible.

@ZZZ21321,
You can extract math equations from a presentation like this:

math_paragraph = auto_shape.text_frame.paragraphs[0].portions[0].math_paragraph

More examples:

As for converting math equations to LaTeX, we have opened the following new ticket(s) in our internal issue tracking system and will consider the feature according to the terms mentioned in Free Support Policies.

Issue ID(s): SLIDESPYNET-131

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

With Aspose.Slides for Python, you can convert a presentation to HTML documents like this:

presentation.save("output1.html", SaveFormat.HTML)
presentation.save("output2.html", SaveFormat.HTML5)

More examples:

Thanks, I have 2 questions

  1. how to for loop extract all math equations to mathml?
  2. replace the math equations to mathm text result

I see there is a option in aspose.words can convert it
HtmlOfficeMathOutputMode enumeration | Aspose.Words for Python, I don’t know if slides have it?

There I also see the math equations can be converted to mathml, can it convert in memory?Exporting Math Equations|Aspose.Slides for Python Documentation

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as pres:
    autoShape = pres.slides[0].shapes.add_math_shape(0, 0, 500, 50)
    mathParagraph = autoShape.text_frame.paragraphs[0].portions[0].math_paragraph

    mathParagraph.add(
        math.MathematicalText("a").
            set_superscript("2").
            join("+").
            join(math.MathematicalText("b").set_superscript("2")).
            join("=").
            join(math.MathematicalText("c").set_superscript("2")))

    with open("mathml.xml", "wb") as stream:
        mathParagraph.write_as_math_ml(stream)

So I want to get all math equations in PPT slides and convert it to mathml?

@ZZZ21321,
Thank you for posting the questions, I will get back to you as soon as possible.

@ZZZ21321,

You can use the for operator to go through all slides to find auto shapes, then go through all paragraphs and text portions in the auto shapes to find all MathPortion objects, and extract math equations using the write_as_math_ml method.

You can remove math paragraphs from MathPortion objects and add your own equations in their place. Please see the following article:

Please try using the BytesIO or StringIO class from the io module in Python.

import io
import aspose.slides as slides

from aspose.slides import AutoShape
from aspose.slides.mathtext import MathPortion

with slides.Presentation("sample.pptx") as presentation:
    
    for slide in presentation.slides:
        for shape in slide.shapes:

            if not isinstance(shape, AutoShape):
                continue

            for paragraph in shape.text_frame.paragraphs:
                for portion in paragraph.portions:

                    if not isinstance(portion, MathPortion):
                        continue

                    data = io.BytesIO()
                    portion.math_paragraph.write_as_math_ml(data)

Please let us know if you have any further questions.

Thanks, I am also looking this sample. @andrey.potapov can you give whole sample code?

@kivenlun,
I’ve updated the post above with the sample code.

Thanks @andrey.potapov. Let me show your a sample.
input, there is a math equations in paragraph, maybe EQ1
output, I want to convert EQ1 into mathml string and save PPT to output.pptx. and the origin place for EQ1 has been replaced by mathml EQ1

AttributeError: attribute ‘math_paragraph’ of ‘aspose.slides.mathtext.MathPortion’ objects is not writable

@kivenlun,
Could you please create a new forum thread and share the following files and information?

  • sample presentation file
  • code example that reproduces the error
  • OS version on which the code was executed
  • Aspose.Slides version you used

The issues you found earlier (filed as SLIDESPYNET-131) have been fixed in Aspose.Slides for Python 24.4 (Windows x86, Windows x64, Linux, macOS x86-x64, macOS ARM64).
You can check all fixes on the Release Notes page.
You can also find the latest version of our library on the Product Download page or PyPI.