Python将Latex公式写入word

安装

pip install latex2word

示例

from docx import Document
from latex2word import LatexToWordElement

"""
`pip install python-docx`  
常规用法  
General usage.  
"""

# 公式(Latex string)
latex_input = r"x={-b \pm \sqrt{b^2-4ac}\over 2a}"
# 构建latex_to_word对象 (Build latex_to_word objects)
latex_to_word = LatexToWordElement(latex_input)

# 插入word公式(Create the docx file and insert the element)
doc = Document()
paragraph = doc.add_paragraph()
latex_to_word.add_latex_to_paragraph(paragraph)

doc.save('test.docx')

结果

设置字体大小和颜色

from docx import Document
from docx.shared import Pt, RGBColor
from latex2word import LatexToWordElement

"""
`pip install python-docx`  
设置字体大小和颜色  
Set the font size and color.  
"""

# Issue #1: ref: https://github.com/Gu-f/LatexToWord/issues/1

# 公式(Latex string)
latex_input = r"x={-b \pm \sqrt{b^2-4ac}\over 2a}"
# 构建latex_to_word对象 (Build latex_to_word objects)
latex_to_word = LatexToWordElement(latex_input)

# 插入word公式(Create the docx file and insert the element)
doc = Document()
paragraph = doc.add_paragraph()

paragraph.style.font.size = Pt(24)  # 设置字号大小(Set the font size)
paragraph.style.font.color.rgb = RGBColor(255, 0, 0)  # 设置颜色(Set the font color)

latex_to_word.add_latex_to_paragraph(paragraph)

doc.save('test.docx')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值