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
How to add math formulas in odt document? #22
Comments
I've solved this problem by writing a few functions. Is it possible to include these functions in opfpy in the next release? #!/usr/bin/env python
# -*- coding: utf-8 -*-
from xml.dom.minidom import parseString
from xml.dom import Node
import odf
import odf.opendocument
import odf.text
from odf.element import Element
from namespaces import MATHNS
math_templ = u'\
<math xmlns="http://www.w3.org/1998/Math/MathML">\
<semantics>\
<annotation encoding="StarMath 5.0">%s</annotation>\
</semantics></math>'
def gen_odf_math_(parent):
elem = Element(qname = (MATHNS,parent.tagName))
if parent.attributes:
for attr, value in parent.attributes.items():
elem.setAttribute((MATHNS,attr), value, check_grammar=False)
for child in parent.childNodes:
if child.nodeType == Node.TEXT_NODE:
text = child.nodeValue
elem.addText(text, check_grammar=False)
else:
elem.addElement(gen_odf_math_(child), check_grammar=False)
return elem
def gen_odf_math(starmath_string):
u'''
Generating odf.math.Math element
'''
mathml = math_templ % (starmath_string)
math_ = parseString(mathml.encode('utf-8'))
math_ = math_.documentElement
odf_math = gen_odf_math_(math_)
return odf_math
def main():
doc = odf.opendocument.OpenDocumentText()
p = odf.text.P(text=u'text')
df = odf.draw.Frame( zindex=0, anchortype='as-char')
p.addElement(df)
doc.text.addElement(p)
formula = 'c = sqrt(a^2+b_2) + %ialpha over %ibeta'
math = gen_odf_math(formula)
do = odf.draw.Object()
do.addElement(math)
df.addElement(do)
outputfile = u'result'
doc.save(outputfile, True)
if __name__ == '__main__':
main() |
A more convenient way #!/usr/bin/env python
# -*- coding: utf-8 -*-
import odf
from odf.opendocument import OpenDocumentText
from odf.element import Element
from odf.text import P
from odf.math import Math
from namespaces import MATHNS
def main():
doc = OpenDocumentText()
p = P(text=u'text')
df = odf.draw.Frame( zindex=0, anchortype='as-char')
p.addElement(df)
doc.text.addElement(p)
formula =u'c=sqrt(a^2+b^2)'
math = Math()
annot = Element(qname = (MATHNS,u'annotation'))
annot.addText(formula, check_grammar=False)
annot.setAttribute((MATHNS,'encoding'), 'StarMath 5.0', check_grammar=False)
math.addElement(annot)
do = odf.draw.Object()
do.addElement(math)
df.addElement(do)
outputfile = u'result'
doc.save(outputfile, True)
if __name__ == '__main__':
main() |
I tried the solutions given but a problem remains : in the document "result.odt", the formula is a little frame and its content is invisible. I must edit it by double-clicking on it to see it. |
Hello @ntala , |
@scizers This is bug in openoffice https://forum.openoffice.org/en/forum/viewtopic.php?p=35596 |
@zimakim
|
@scizers
|
I need to add mathematical formulas in document. I have not found an example of how to do it
The text was updated successfully, but these errors were encountered: