0

I need to add mathematical formulas in odt document. I have not found an example of how to do it. I tried the following code. But it generates an empty formula. I don't know how to add to it something like c = a + b. Somebody solved a similar problem? The formula should be written by MathML code. But I have no idea where to insert it here.

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

import odf
import odf.opendocument
import odf.math
import odf.text

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)

    math = odf.math.Math()
    do = odf.draw.Object()
    do.addElement(math)
    df.addElement(do)

    outputfile = u'result'
    doc.save(outputfile, True)
if __name__ == '__main__':
    main()
user3698839
  • 21
  • 10

1 Answers1

0
#!/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()
user3698839
  • 21
  • 10
  • I know the answer is 6 years ago. Any documents on the formula string? I mean, how to write fraction, integral, matrix, and so on. Btw, it seems that there is still no pacakge which support wrting formula into document. – oyster Dec 18 '21 at 14:59