You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Insert MathML as plain text for Word 2010 to render it as an equation #320
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:
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.
Reacted by Bruno Herfst, Vadym Bashkarov, Alex Sun, Francis Niu 牛飞龙, CharlieYu1, alingse, l00c00l, zackie, Reem Alghamdi, The Romantic Dev and 1 more
@peepall It has been a while since you made this post, but I was hoping you could help me out. I am working with the following exact snippet:
from docx import Document
from lxml import etree
mathml_string = '<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mn>1</mn><mn>2</mn></mfrac></math>' # From the first post
# 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)
doc = Document()
paragraph = doc.add_paragraph()
paragraph._element.append(new_dom.getroot())
doc.save('test.docx')
The issue is that new_dom appears to be an empty result and so new_dom.getroot() returns None. The actual error is from append:
TypeError: Argument 'element' has incorrect type (expected lxml.etree._Element, got NoneType)
You will find the MML2OMML.XSL file from Office 14 (2010) below. The content of the file you are using is different and may cause an issue during data transformation.
Using the code provided by @peepall and some sympy(https://docs.sympy.org) funcionalities I made a simple function that makes it easier to output equations to word! Here it is:
from lxml import etree
from sympy.printing.mathml import mathml
from sympy import *
def math_to_word(eq, equal=None):
"""Transform a sympy equation to be printed in word document."""
# Mathml uses sympy to transform equations to mathml format
# eq = equation you want to add to Word using sympy format
# The math_to_wrod functions also have an optitional equal argument
# that prints an = sign and the number passed to the argument
math_ml = mathml(eq, printer='presentation')
# Creates mathml string
if equal is None:
mathml_string = '''
<math xmlns="http://www.w3.org/1998/Math/MathML">
{}
</math>
'''.format(math_ml)
else:
mathml_string = '''
<math xmlns="http://www.w3.org/1998/Math/MathML">
{0}
<mo>=</mo>
<mn>{1}</mn>
</math>
'''.format(math_ml, equal)
# Converts mathml string
tree = etree.fromstring(mathml_string)
xslt = etree.parse(
mml2omml_stylesheet_path
)
transform = etree.XSLT(xslt)
new_dom = transform(tree)
return new_dom.getroot()
A example of usage within a docx script is:
from docx import Document
from math_to_word import math_to_word
from sympy.abc import x
doc = Document()
p = doc.add_paragraph()
p._element.append(math_to_word(x+1, equal=10))
doc.save('test1.docx')
@peepall Thanks for providing this excellent solution! As others have stated, it works like a charm.
I'm trying to perform a similar operation using python-pptx. I created the following as a test, and while it runs successfully, my resulting output doesn't contain any equation.
from pptx import Presentation
from pptx.util import Inches, Pt
from lxml import etree
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
tree = etree.fromstring('<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>=</mo><mstyle displaystyle="true"><mfrac><mrow><mrow><mo>−</mo><mi>b</mi></mrow><mo>±</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mrow><mn>4</mn><mo>⁢</mo><mi>a</mi><mo>⁢</mo><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mo>⁢</mo><mi>a</mi></mrow></mfrac></mstyle></math>')
xslt = etree.parse('C:/Program Files/Microsoft Office/root/Office16/MML2OMML.XSL')
transform = etree.XSLT(xslt)
new_dom = transform(tree)
p = tf.add_paragraph()
p._element.append(new_dom.getroot())
prs.save('testDoc.pptx')
@peepall Do you have any sage advice regarding performing this operation in python-pptx?
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.
Hi!
I'm also working on this issue and thanks to all you I got it, but there is another problem now!
I can't increase font size of the MathML equations (that inserted in MS Word by Python-docx).
Standart MathML attributes like mathsize or maxsize or scriptminsize don't work.
There are no any problems with the running of code but MathML equation has CambriaMath font with font size 11 (I need to make it 14).
Any others attributes for setup the math font (like fontweight or fontcolor others) don't work.
Great solution, thanks! The only question is now about the XSL file - I could not manage to find the redistribution policy for this file or any other Office-included XSL file. Does anyone know about open-licensed version of MML2OMML.XSL - or the redistribution policy of it?
Hi all, need some pointers with issues I am facing while trying to write math expressions/equations with a mix of text to Word document using solution approach given in zimakim's post (dated 14th March 2021). I am using latex2mathml and p._element.append(). The issues I am facing in the output are -
The output is not wrapping to next line, but going beyond document page boundary
The output is center justified, whereas I need it left aligned
The output in one case is not inline and breaking to next line at "=" sign. This behavior is also not as seen in issue 1 above.
I am not able to figure out the root cause of these issues. I am not able to locate much documentation on _element and how to use it in Python programs. I have attached the Word document output that is generated by my program. Any help to resolve these issues is highly appreciated.
This code creates a new .docx document, adds a paragraph to it, and inserts a mathematical formula specified in LaTeX format into that paragraph. The document is then saved as new_file.docx.
Activity
scizers commentedon Mar 21, 2017
Hello @peepall,
Any success with inserting malthml ?
peepall commentedon 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.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.scizers commentedon Mar 22, 2017
Thanks @peepall,
Worked like a charm !
madphysicist commentedon Nov 7, 2017
@peepall It has been a while since you made this post, but I was hoping you could help me out. I am working with the following exact snippet:
The issue is that
new_domappears to be an empty result and sonew_dom.getroot()returnsNone. The actual error is fromappend:This is probably related to the style sheet I am using. I obtained my copy of
mml2omml.xslfrom https://github.com/MartinPaulEve/meTypeset/blob/af0b1ad33a3280150a53ba20afe97285121d1300/docx/utils/maths/mml2omml.xsl (this is the current master branch, I just wanted to fix it to a specific commit for stability in future referencing).I also tried to remove the
xmlnslike this:mathml_string = '<math><mfrac><mn>1</mn><mn>2</mn></mfrac></math>'.What am I doing wrong and how do I fix it?
peepall commentedon Nov 8, 2017
Hi @madphysicist
You will find the
MML2OMML.XSLfile from Office 14 (2010) below. The content of the file you are using is different and may cause an issue during data transformation.MML2OMML.zip
Tell me if this solves your issue.
Regards
madphysicist commentedon Nov 8, 2017
@peepall It certainly did. Many thanks!
requeijaum commentedon Mar 14, 2018
👀
Daniel-Quistan commentedon Jan 22, 2019
Hello!
Using the code provided by @peepall and some sympy(https://docs.sympy.org) funcionalities I made a simple function that makes it easier to output equations to word! Here it is:
A example of usage within a docx script is:
Hope it will be useful! Thanks @peepall
kencomputes commentedon Jul 11, 2019
@peepall Thanks for providing this excellent solution! As others have stated, it works like a charm.
I'm trying to perform a similar operation using python-pptx. I created the following as a test, and while it runs successfully, my resulting output doesn't contain any equation.
@peepall Do you have any sage advice regarding performing this operation in python-pptx?
kencomputes commentedon Jul 15, 2019
If anyone on a similar mission comes across this post, here's how I got things working.
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.
Vaclav-Laurin commentedon Jul 23, 2020
Hi!
I'm also working on this issue and thanks to all you I got it, but there is another problem now!
I can't increase font size of the MathML equations (that inserted in MS Word by Python-docx).
Standart MathML attributes like mathsize or maxsize or scriptminsize don't work.
There are no any problems with the running of code but MathML equation has CambriaMath font with font size 11 (I need to make it 14).
Any others attributes for setup the math font (like fontweight or fontcolor others) don't work.
What I do wrong?
Here is an example of code that I used:
zimakim commentedon Mar 13, 2021
I solved this issue with the latex2mathml module. Please buy coffee for the man who wrote it
scanny commentedon Mar 13, 2021
Nice job Akim :)
UHSA94 commentedon Apr 26, 2021
thanks,now i can add a math equation to docx , i can also set text's font and color,but how could i set font size and color for a math equation?
AsadAmeer7 commentedon Feb 18, 2022
Hi everybody,
I am trying to read equation as linear text form(latex) and images from word document in python script using
python-docxanyone here have any idea on it?
retsyo commentedon Apr 8, 2022
some MathML string can not be rendered correctly in DOCX( or MS Word?). for example
roniemartinez/latex2mathml#298
roniemartinez/latex2mathml#295
roniemartinez/latex2mathml#292
roniemartinez/latex2mathml#290
roniemartinez/latex2mathml#317
retsyo commentedon Apr 8, 2022
some, for example
\sum,\intand\prod, has flawroniemartinez/latex2mathml#288
fathoni commentedon Aug 26, 2022
Great solution, thanks! The only question is now about the XSL file - I could not manage to find the redistribution policy for this file or any other Office-included XSL file. Does anyone know about open-licensed version of MML2OMML.XSL - or the redistribution policy of it?
shashikulk commentedon Mar 11, 2023
Hi all, need some pointers with issues I am facing while trying to write math expressions/equations with a mix of text to Word document using solution approach given in zimakim's post (dated 14th March 2021). I am using latex2mathml and p._element.append(). The issues I am facing in the output are -
I am not able to figure out the root cause of these issues. I am not able to locate much documentation on _element and how to use it in Python programs. I have attached the Word document output that is generated by my program. Any help to resolve these issues is highly appreciated.
Trial_Word_Output.docx
Gu-f commentedon Sep 10, 2024
Another alternative: LatexToWord
A pure python library implemented by python3 for writing Latex formulas to word.
StephanMeijer commentedon Oct 1, 2024
@shashikulk Can I use your file https://github.com/python-openxml/python-docx/files/10948322/Trial_Word_Output.docx for testing purposes in my project, and republish in my Git repository, in order to support conversions between specifications of mathematics?
shashikulk commentedon Oct 7, 2024
@StephanMeijer sure you can.
keh9mark commentedon Jan 29, 2025
To add a formula (equation) to a .docx document, you can use the math2docx library. Here is an example code that demonstrates how to do this:
import math2docx from docx import Document
document = Document() p = document.add_paragraph()
#LaTeX Math Formula latex_ = r"\cos (2\theta) = \cos^2 \theta - \sin^2 \theta"
math2docx.add_math(p, latex_) document.save("new_file.docx")
This code creates a new .docx document, adds a paragraph to it, and inserts a mathematical formula specified in LaTeX format into that paragraph. The document is then saved as new_file.docx.
https://github.com/keh9mark/math2docx