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

相关推荐
Metaphor69235 分钟前
使用 Python 给 PDF 设置背景色或背景图
数据库·python·pdf
郝亚军1 小时前
如何让pycharm-2026.1.2顶部菜单栏固定显示在最上端
python
怪兽学LLM1 小时前
LeetCode 438 找到字符串中所有字母异位词(Python 固定滑动窗口+字符计数解法)
python·算法·leetcode
麻雀飞吧1 小时前
期货量化日志别泄露密码:天勤账户凭证脱敏写法
python
CC数学建模1 小时前
2026年江西省研究生数学建模竞赛1题:空间数据分析中的过拟合识别完整思路、代码、模型、文章,全网首发高质量分享!
python·算法·数学建模
matlabgoodboy1 小时前
计算机java程序代写python代码编写c/c++代做qt设计php开发matlab
java·c语言·python
不考研当牛马1 小时前
Django 框架 深度学习
python·深度学习·django
databook1 小时前
用SymPy自动求解追及问题的方程
python·数学·动效
测试19981 小时前
Jmeter性能压测:TPS与QPS
自动化测试·软件测试·python·jmeter·测试用例·压力测试·性能测试