如何使用 odfpy 在 odt 文档中添加数学公式?
How to add math formulas in odt document using odfpy?
我需要在odt文档中添加数学公式。我还没有找到如何做的例子。我尝试了以下代码。但它会生成一个空公式。我不知道如何添加类似 c = a + b 的内容。有人解决过类似的问题吗?公式应使用 MathML 代码编写。但是我不知道在哪里插入它。
#!/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()
#!/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()
我需要在odt文档中添加数学公式。我还没有找到如何做的例子。我尝试了以下代码。但它会生成一个空公式。我不知道如何添加类似 c = a + b 的内容。有人解决过类似的问题吗?公式应使用 MathML 代码编写。但是我不知道在哪里插入它。
#!/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()
#!/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()