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后的结果:

相关推荐
Mikhail_G9 分钟前
Python应用八股文
大数据·运维·开发语言·python·数据分析
mikes zhang10 分钟前
Flask文件上传与异常处理完全指南
后端·python·flask
烛阴20 分钟前
深入浅出地理解Python元类【从入门到精通】
前端·python
weixin_464078071 小时前
Python学习小结
python·学习
ubax2 小时前
day 51 python打卡
开发语言·python
laocooon5238578862 小时前
基于Python的TCP应用案例,包含**服务器端**和**客户端**的完整代码
网络·python·tcp/ip
哆啦A梦的口袋呀2 小时前
设计模式汇总
python·设计模式
救救孩子把3 小时前
如何在n8n中突破Python库限制,实现持久化虚拟环境自由调用
开发语言·python·n8n
测试19983 小时前
2025软件测试面试题汇总(接口测试篇)
自动化测试·软件测试·python·测试工具·面试·职场和发展·接口测试
抽风的雨6104 小时前
【python深度学习】Day53 对抗生成网络
python·深度学习