我想使用python在WORD中插入分数,如下图所示。我只能插入这些: 15/20 + 4/20。我想让样式看起来像图片显示的样子。是否可以使用Python或Python中的其他库来完成它呢?

发布于 2022-02-02 16:55:50
Python没有实现用于处理Word公式的高级API,但是如果您可以自己构造XML字符串,则可以将其插入文档中。XML是Microsoft,在概念上类似于MathML。
from docx import Document
from docx.oxml import parse_xml
document = Document()
p = document.add_paragraph()
omml_xml = '<p xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"><m:oMathPara><m:oMath><m:f><m:num><m:r><m:t>1</m:t></m:r></m:num><m:den><m:r><m:t>2</m:t></m:r></m:den></m:f></m:oMath></m:oMathPara></p>'
omml_el = parse_xml(omml_xml)[0]
p._p.append(omml_el)
document.save('demo.docx')下面是OMML片段:
<p xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<m:oMathPara>
<m:oMath>
<m:f>
<m:num>
<m:r>
<m:t>1</m:t>
</m:r>
</m:num>
<m:den>
<m:r>
<m:t>2</m:t>
</m:r>
</m:den>
</m:f>
</m:oMath>
</m:oMathPara>
</p>它生成一个分数为1/2的单词doc。

https://stackoverflow.com/questions/70946715
复制相似问题