Python将Latex公式插入到Word中

推荐一个库,可以使用python将Latex公式插入到Word中显示

使用pip进行安装: pip install latex2word

示例将如下公式插入到word

公式1:

f(x) = \int_{-\infty}^\infty  \hat f(x)\xi\,e^{2 \pi i \xi x}  \,\mathrm{d}\xi 

公式2:

\int x^{\mu}\mathrm{d}x=\frac{x^{\mu +1}}{\mu +1}+C, \left({\mu \neq -1}\right) 

公式3:

\int u \frac{\mathrm{d}v}{\mathrm{d}x}\,\mathrm{d}x=uv-\int \frac{\mathrm{d}u}{\mathrm{d}x}v\,\mathrm{d}x 
from docx import Document
from latex2word import LatexToWordElement

latex_input1 = r"f(x) = \int_{-\infty}^\infty  \hat f(x)\xi\,e^{2 \pi i \xi x}  \,\mathrm{d}\xi "
latex_to_word1 = LatexToWordElement(latex_input1)

latex_input2 = r"\int x^{\mu}\mathrm{d}x=\frac{x^{\mu +1}}{\mu +1}+C, \left({\mu \neq -1}\right) "
latex_to_word2 = LatexToWordElement(latex_input2)

latex_input3 = r"\int u \frac{\mathrm{d}v}{\mathrm{d}x}\,\mathrm{d}x=uv-\int \frac{\mathrm{d}u}{\mathrm{d}x}v\,\mathrm{d}x "
latex_to_word3 = LatexToWordElement(latex_input3)

doc = Document()
paragraph = doc.add_paragraph()

latex_to_word1.add_latex_to_paragraph(paragraph)
paragraph.add_run(f"\n{'======伟大的分割线======'*3}\n")
latex_to_word2.add_latex_to_paragraph(paragraph)
paragraph.add_run(f"\n{'======伟大的分割线======'*3}\n")
latex_to_word3.add_latex_to_paragraph(paragraph)

doc.save('latex.docx')

插入Word后的结果:

Python将Latex公式插入到Word中_第1张图片

你可能感兴趣的:(word,python,latex,latex2word)