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

相关推荐
程序员小远4 分钟前
接口测试和单元测试详解
自动化测试·软件测试·python·测试工具·单元测试·测试用例·接口测试
Tech Synapse13 分钟前
电商商品推荐系统实战:基于TensorFlow Recommenders构建智能推荐引擎
人工智能·python·tensorflow
聿小翼17 分钟前
selenium-wire 与 googletrans 的爱恨情仇
python
咖啡调调。19 分钟前
模板引擎语法-算术运算
python·django·sqlite
CodeCraft Studio19 分钟前
Excel处理控件Spire.XLS系列教程:Java设置Excel活动工作表或活动单元格
java·python·excel
Doker 多克20 分钟前
Python-Django系列—部件
开发语言·python
Linux运维老纪22 分钟前
Python文件操作及数据库交互(Python File Manipulation and Database Interaction)
linux·服务器·数据库·python·云计算·运维开发
Bruce_Liuxiaowei28 分钟前
MCP Python SDK构建的**SQLite浏览器**的完整操作指南
数据库·python·sqlite
q_q王28 分钟前
实时数字人——DH_LIVE
python·大模型·数字人·实时
子燕若水36 分钟前
解释PyTorch中的广播机制
人工智能·pytorch·python