-
Notifications
You must be signed in to change notification settings - Fork 501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inserting OMML into Text Frame or Paragraph #528
Comments
The prior step here is to add an equation to PowerPoint by hand, using the equation editor, and then examine the XML that produces (that works). It helps a lot to make the example presentation as simple as possible, so one slide with one shape. Then you can find the XML in question with: $ opc browse my-example.pptx slide1.xml |
Thank you for the support @scanny! I was able to append the math element with the following code:
If there are text nodes to be inserted before/after the math content that can be accomplished like this:
I'm still working out some kinks, but this should be a good starting point for whoever might be attempting this in the future. |
Hey there, this was really helpful thanks, saved me so much time. import re
import latex2mathml.converter
from pptx import Presentation
from docx import Document
from docx.shared import Inches
import docxlatex as latex
from lxml import etree
def latex_to_word(latex_input, for_ppt=False):
mathml = latex2mathml.converter.convert(latex_input, display="block")
tree = etree.fromstring(mathml)
xslt = etree.parse(
'D:\Programming\python\pptxMathVisualize\src\MML2OMML.XSL'
)
transform = etree.XSLT(xslt)
new_dom = transform(tree)
if for_ppt:
wrapper = etree.fromstring(
'''<a14:m xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main">
<m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
</m:oMath>
</a14:m>''')
wrapper.getchildren()[0].append(new_dom.getroot())
return wrapper
return new_dom.getroot()
def getLatexEquations(text):
return re.findall(r'\$([^$].*?)\$', text)
document = Document('demo.docx')
latexDoc = latex.Document('demo.docx')
prs = Presentation()
title_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(title_slide_layout)
left = top = width = height = Inches(1)
equations = getLatexEquations(latexDoc.get_text())
for equation in equations:
p = document.add_paragraph()
p._element.append(latex_to_word(equation))
txBoxNum = equations.index(equation) + 1.5
txBox = slide.shapes.add_textbox(left, Inches(1 * txBoxNum), width, height)
tf = txBox.text_frame
txBoxP = tf.add_paragraph()
txBoxP._element.append(latex_to_word(equation, for_ppt=True))
tf
prs.save('test.pptx')
document.save('test1.docx') Sorry haven't had time to refactor, so let me explain the code a little, basically we have two doc variables, latexDoc is for importing the formula from the docx, the other one is for editing the docx file, and the prs variable is self-explanatory, I am importing the equations using regex to separate them from text, and then putting them in text boxes in the pptx slide. |
Hello,
I am trying to build a pipeline to convert existing MathML to OMML and insert it into a text frame in PPT.
I came across a very useful post regarding inserting MathML into a Word doc with python-docx.
It involves performing an XSL transformation on the plain MML using Office's "MML2OMML.XSL", then appending that etree object to a new paragraph. (python-openxml/python-docx#320)
Here's an example that works for me with python-docx:
I tried something similar using python-pptx. It runs without throwing any errors and creates the specified file, however the created document contains no equation.
Here's my attempt with python-pptx:
Clearly I'm doing something wrong here, but I'm not quite sure what.
I simplified my MathML to
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi></math>
and manually inserted the resulting OMML into a blank PPT slide then compared the slide1.xml between an empty slide & the slide with the formula.Here's the XML that was added to the slide when I inserted the formula:
Does anyone have any clever ideas for how I might go about inserting a formula into a blank slide/text field/paragraph?
The text was updated successfully, but these errors were encountered: