question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Insert MathML as plain text for Word 2010 to render it as an equation

See original GitHub issue

Hi everybody,

I am trying to insert some MathML code into a *.docx document. To do so, I do the following:

#!/bin/env python
# -*- coding: utf8 -*-

from docx import Document


doc = Document('template.docx')

# Word needs a specific XML namespace in the '<math>' tag in order to embed MathML text into a math area
# Also, the MathML expression must contain no newline or Word will create one distinct math area for each line
math_ml = '<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mn>1</mn><mn>2</mn></mfrac></math>'

# Insert the MathML text into Word
doc.add_paragraph().add_run(text=math_ml, style=None).font.math = True
doc.save('test.docx')

When I open the resulting document in Word 2010, this is what I see. The math area has been correctly created, but the code itself has not been rendered.

The interesting thing is when I open the resulting document and then copy then paste the MathML code from/to the math area using “Paste as plain text” directly from Word UI, it gets properly rendered.

I have come to the conclusion that when a style is applied to the MathML text, then Word does not render it as an equation. And this assertion is verified by the fact that when you use another paste option like “Keep source formatting” or “Melt both formats”, then the equation is not rendered using Word UI.

My question is: how can I insert the MathML text as plain text into Word without any style using python-docx lib ? I tried those, but none of them worked:

doc.add_paragraph().add_run(text=math_ml, style=None).font.math = True
doc.add_paragraph().add_run(text=math_ml, style='').font.math = True
doc.add_paragraph().add_run(text=math_ml).font.math = True

Or maybe this is currently not supported by this library ?

Thanks for your help 😃

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:5
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
zimakimcommented, Mar 13, 2021

I solved this issue with the latex2mathml module. Please buy coffee for the man who wrote it

from docx import Document
from lxml import etree
import latex2mathml.converter


def latex_to_word(latex_input):
    mathml = latex2mathml.converter.convert(latex_input)
    tree = etree.fromstring(mathml)
    xslt = etree.parse(
        'MML2OMML.XSL'
        )
    transform = etree.XSLT(xslt)
    new_dom = transform(tree)
    return new_dom.getroot()


document = Document()

p = document.add_paragraph()
word_math = latex_to_word(ur"\sum_{i=1}^{10}{\frac{\sigma_{zp,i}}{E_i} kN")
p._element.append(word_math)

p = document.add_paragraph()
p.add_run('before formula ')
p._element.append(word_math)
p.add_run(' after formula ')

p = document.add_paragraph()
word_math = latex_to_word(ur"\sum_{n=1}^{10}{n^{2}}")
p._element.append(word_math)

p = document.add_paragraph()
word_math = latex_to_word(ur"\sqrt{\frac{a}{b}}")
p._element.append(word_math)


p = document.add_paragraph()
word_math = latex_to_word(ur"\begin{matrix*}[r]a & b \\ c & d \end{matrix*}")
p._element.append(word_math)

document.save('demo.docx')
8reactions
peepallcommented, Mar 21, 2017

Hi @scizers ,

I’ve managed to make it work. However, you must use the MathML to Office MathML stylesheet in order to transform the MathML data into Office-compatible MathML.

The stylesheet is installed along with Microsoft Office and you can find it in: C:\Program Files (x86)\Microsoft Office\Office14\MML2OMML.XSL.

#!/bin/env python
# coding: utf-8

from lxml import etree

# Convert MathML (MML) into Office MathML (OMML) using a XSLT stylesheet
tree = etree.fromstring(mathml_string)
xslt = etree.parse(mml2omml_stylesheet_path)

transform = etree.XSLT(xslt)
new_dom = transform(tree)

paragraph = doc.add_paragraph()
paragraph._element.append(new_dom.getroot())

You may also need to remove any namespace from the <math> tag before transforming it into Office MathML. Let me know if it works for you too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can you import MathML into Word? - Microsoft Q&A
I am trying to find out if it is possible to import MathML (from an HTML page) into Word with either MathType or...
Read more >
Add Math Equations With Word 2010 Equation Editor
Launch Word 2010 document, in which you want to insert any mathematical equation. Now navigate to Insert tab, and Click Equation drop-down ...
Read more >
Copy MathML into Word to use as equation - Stack Overflow
Word online supports displaying MathML, but not interpreting or inserting it (as of June 2018). You'll have to paste it in full client...
Read more >
Copy pasting Math Formula in Word | Latex in Word | MathML
In this video, we look at how we can copy and paste a math formula in word. We explore the insert equation feature...
Read more >
Ms Word tutorial on how to type / insert equation in ... - YouTube
Ms Word tutorial on how to type / insert equation in Ms Word : Three different ways. It includes how to type equation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found