我将此作为 python-docx 问题提交: https: //github.com/python-openxml/python-docx/issues/805,但被要求在此处展开讨论。
https://python-docx.readthedocs.io/en/latest/user/styles-using.html意味着我应该能够更改标题字体样式,如下所示:
font = doc.styles['Heading 1'].font
font.name = 'Times New Roman'
font.size = docx.shared.Pt(16)
Run Code Online (Sandbox Code Playgroud)
但这不起作用:生成的文档对所有标题都使用 Calibri。(它们也是蓝色的,标题 1 有下划线,我也需要以某种方式消除它。)
它也无法更改特定标题上的字体,也无法删除标题的 Latent_styles 。
下面是一个测试程序,它尝试了所有三种方法,但标题 1 和 2 仍然显示为蓝色 Calibri,尽管所有尝试都将其更改为 Times New Roman:
import docx
doc = docx.Document()
# Deleting heading latent styles seems to do nothing:
latent_styles = doc.styles.latent_styles
latent_styles['Heading 1'].delete()
latent_styles['Heading 2'].delete()
# Setting the Normal font works:
font = doc.styles['Normal'].font
font.name = 'Times New Roman'
font.size = docx.shared.Pt(12)
# Setting heading styles doesn't do …Run Code Online (Sandbox Code Playgroud)