From 4075bd09f3f14c7b3e9f6a08289f9de2a0a3ddbe Mon Sep 17 00:00:00 2001 From: gabrieldrapor Date: Thu, 26 Mar 2020 17:32:51 +0800 Subject: [PATCH] add mathml support --- ooxml/doc.py | 21 ++++++++++++++++++--- ooxml/parse.py | 4 ++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ooxml/doc.py b/ooxml/doc.py index 74a776b..c290ea6 100644 --- a/ooxml/doc.py +++ b/ooxml/doc.py @@ -8,6 +8,8 @@ import six import collections +from dwml import omml +from lxml import etree class Style(object): @@ -476,11 +478,24 @@ class Math(Element): Math elements are not supported at the moment. We just parse them and create empty element.""" - def __init__(self): - pass + def __init__(self, element, tag): + mathml = etree.tostring(element).decode() + if tag == 'oMath': + mathml = """ + %s + """ % mathml + + latexs = [i.latex for i in omml.load_string(mathml)] + if latexs: + self.latex = '${0}$'.format(latexs[0]) + else: + self.latex = '' def value(self): - return '' + return self.latex class SmartTag(Element): diff --git a/ooxml/parse.py b/ooxml/parse.py index 94d73b8..e2b03dc 100644 --- a/ooxml/parse.py +++ b/ooxml/parse.py @@ -327,11 +327,11 @@ def parse_paragraph(document, par): parse_text(document, paragraph, elem) if elem.tag == _name('{{{m}}}oMath'): - _m = doc.Math() + _m = doc.Math(elem, tag='oMath') paragraph.elements.append(_m) if elem.tag == _name('{{{m}}}oMathPara'): - _m = doc.Math() + _m = doc.Math(elem, tag='oMathPara') paragraph.elements.append(_m) if elem.tag == _name('{{{w}}}commentRangeStart'):