使用docx更改字体

2024-06-16 09:20:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在word文档中写两个句子,每个句子使用不同的字体,但是当我运行此代码时:

import docx
import os
from docx.shared import Inches
from docx.shared import Pt

normfont = doc.styles['Normal']
font = normfont.font
font.name = 'Arial'
font.size = Pt(25)
arialparagraph= doc.add_paragraph()
arialparagraph.style = doc.styles['Normal']
arialparagraph.add_run("This line is using one font")


lcd = doc.styles['Normal']
font = lcd.font
font.name = 'Modern LCD-7'
font.size = Pt(30)
lcdparagraph = doc.add_paragraph()
lcdparagraph.style = doc.styles['Normal']
lcdparagraph.add_run("This line is using a different font")

整个文档仅使用最后定义的字体(现代LCD-7)

这是因为我正在覆盖正常样式。有没有办法创造我自己的风格?例如doc.style['LCD']


Tags: 文档importaddptdoclcdstyle字体