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 
python 复制代码
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后的结果:

相关推荐
可触的未来,发芽的智生18 小时前
新奇特:神经网络速比器,小镇债务清零的算法奇缘
javascript·人工智能·python
mortimer19 小时前
还在被 Windows 路径的大小写和正反斜杠坑?是时候让 pathlib 拯救你的代码了!
人工智能·python
std8602119 小时前
Rust 与 Python – 这是未来的语言吗?
开发语言·python·rust
鄃鳕19 小时前
Flask【python】
后端·python·flask
weixin_466819 小时前
Python编程之面向对象
开发语言·人工智能·python
Lynnxiaowen20 小时前
今天我们学习python编程常用模块与面向对象
运维·python·学习·云计算
一头生产的驴20 小时前
java整合itext pdf实现固定模版pdf导出
java·python·pdf
魔都吴所谓20 小时前
【python】快速实现pdf批量去除指定位置水印
java·python·pdf
YFCodeDream21 小时前
MLLM技术报告 核心创新一览
python·gpt·aigc
夜幽青玄1 天前
mybatis-plus调用报 org.springframework.dao.DataIntegrityViolationException 错误处理
开发语言·python·mybatis