是否有完整的示例将 sympy 中的数学表达式写入 Microsoft Word 文档? [英] Is there a complete example to write a mathematical expression in sympy to a Microsoft Word document?
问题描述
这可能是一个愚蠢的问题.但我很绝望.我是一名数学老师,我尝试生成数学测试.我为此尝试了 Python,并完成了一些事情.然而,我不是一个专业的程序员,所以我对 MathMl、prettyprint() 和任何东西都迷失了.
this might be a silly question. But I am desperate. I am a math teacher and I try to generate Math tests. I tried Python for this and I get some things done. However, I am not a professional programmer, so I get lost with MathMl, prettyprint() and whatsoever.
有没有人可以给我提供一个我可以执行的完整示例?它可能只包含一个愚蠢的小等式,这无关紧要.我只是想看看如何将它放入 Word 文档中.之后,我可以以此为基础.我在 Mac 上工作.
Is there anybody who can supply me a complete example that I can execute? It may just contain one small silly equation, that does not matter. I just want to see how I can get it into a Word document. After that, I can use that as a basis. I work on a Mac.
希望大家帮帮我.提前致谢!
I hope anyone can help me out. Thanks in advance!
最好的问候,约翰
推荐答案
这对我有用:
from sympy import *
from docx import Document
from lxml import etree
# create expression
x, y = symbols('x y')
expr1 = (x+y)**2
# create MathML structure
expr1xml = mathml(expr1, printer = 'presentation')
tree = etree.fromstring('<math xmlns="http://www.w3.org/1998/Math/MathML">'+expr1xml+'</math>')
# convert to MS Office structure
xslt = etree.parse('C:/MML2OMML.XSL')
transform = etree.XSLT(xslt)
new_dom = transform(tree)
# write to docx
document = Document()
p = document.add_paragraph()
p._element.append(new_dom.getroot())
document.save("simpleEq.docx")
这篇关于是否有完整的示例将 sympy 中的数学表达式写入 Microsoft Word 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!