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

相关推荐
CodeCraft Studio14 分钟前
PDF处理控件Aspose.PDF,如何实现企业级PDF处理
java·python·pdf
现实、狠残酷19 分钟前
树莓派5首次开机保姆级教程(无显示器通过VNC连接树莓派桌面)
python
CodeJourney.30 分钟前
用DEEPSEEK做数据看板:高效、实用与创新的融合
数据库·人工智能·python·算法
inxunoffice1 小时前
批量将 Excel 转换 PDF/Word/CSV以及图片等其它格式
pdf·word·excel
Yan-英杰1 小时前
使用阿里云操作系统控制台排查内存溢出
开发语言·python·tcp/ip·阿里云·云计算·deepseek
wangkay881 小时前
基于Python+Vue的智能服装商城管理系统的设计与实现
开发语言·vue.js·python·毕业设计·课程设计
2301_764441331 小时前
使用python自动提取文本关键词
开发语言·python
mofei121382 小时前
Python设计模式 - 建造者模式
python·设计模式·建造者模式
月光技术杂谈2 小时前
llama.cpp 利用intel集成显卡xpu加速推理
人工智能·python·llama·intel·llama.cpp·xpu·集成显卡
byxdaz2 小时前
TensorFlow的pb模型
人工智能·python·tensorflow