无法改变"标题1"使用docx的字体名称

时间:2016-05-13 23:26:15

标签: python-2.7 python-docx

我使用以下脚本:

EnvironmentVariableTarget.Machine

结果是" Header One"得到Calibri'。

1 个答案:

答案 0 :(得分:3)

即使使用python-docx版本0.8.5,这也是一个合法的错误。如果您要更改“Normal”样式的字体名称,它可以正常工作(如python-docx手册中的示例所示),但这不适用于“标题1”样式。

一种解决方法是创建一个新的标题样式,将标题1作为基本样式,然后修改新样式的字体名称&尺寸:

from docx.enum.style import WD_STYLE_TYPE

styles = self.document.styles
new_heading_style = styles.add_style('New Heading', WD_STYLE_TYPE.PARAGRAPH)
new_heading_style.base_style = styles['Heading 1']
font = new_heading_style.font
font.name = 'Arial'
font.size = Pt(16)
self.document.add_paragraph('Header One', style='New Heading')