python - 如何更改 python-docx 中的标题字体和大小?

标签 python docx python-docx

我将此作为 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)

但这不起作用:生成的文档对所有标题都使用 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 anything:
# they all still end up as blue Calibri.
font = doc.styles['Heading 1'].font
font.name = 'Times New Roman'
font.size = docx.shared.Pt(16)

font = doc.styles['Heading 2'].font
font.name = 'Times New Roman'
font.size = docx.shared.Pt(14)

doc.add_heading("Heading 0", 0)
doc.add_paragraph('Here is a paragraph of text.')
doc.add_heading("Heading 1", 1)
doc.add_paragraph('Here is a paragraph of text.')
doc.add_heading("Heading 2", 2)
doc.add_paragraph('Here is a paragraph of text.')

# It also doesn't work to change the style of a specific heading:
heading = doc.add_heading("Another Heading 1", 1)
heading.style.font.name = "Times New Roman"
doc.add_paragraph('Here is a paragraph of text.')

doc.save('test.docx')

Can't change "heading 1" font name using docx提到这是一个错误,并建议创建一种新样式作为解决方法。一个更简单的解决方法是在普通段落文本中创建连续行,然后设置这些连续行的样式。但如果可以将这些标题设置为除蓝色 Calibri 之外的其他内容,那么使用“标题 1”等标准元素似乎会更好......并且文档暗示这是可能的。

最佳答案

正如您所观察到的,通常将字体( font.name )更改为“有效”的样式。由于我不完全理解的原因,Title也许Heading 1 , Heading 2等样式是一个异常(exception)。我希望这与主题指定的字体选择有关。也许这与它们在形成目录方面的特殊作用有关。

首先,有一些观察结果:

  • document.add_heading("0th-level Heading", 0) 应用的样式是 Title 。我认为这在某种程度上是有道理的,因为最高级别的标题赋予了整个文档的标题。样式Heading 1 , Heading 2等适用于 1 时和2分别在该函数调用中使用。

  • 如果我们将字体名称“Times New Roman”应用于 Title样式,然后检查生成的 XML,我们会看到以下内容:

>>> heading = document.add_heading("Title", 0)
>>> title_style = heading.style
>>> title_style.font.name = "Times New Roman"
>>> title_style.element.xml
<w:style xmlns:w=... w:type="paragraph" w:styleId="Title">
  <w:name w:val="Title"/>
  <w:basedOn w:val="Normal"/>
  <w:next w:val="Normal"/>
  <w:link w:val="TitleChar"/>
  <w:uiPriority w:val="10"/>
  <w:qFormat/>
  <w:rsid w:val="00FC693F"/>
  <w:pPr>
    <w:pBdr>
      <w:bottom w:val="single" w:sz="8" w:space="4" w:color="4F81BD" w:themeColor="accent1"/>
    </w:pBdr>
    <w:spacing w:after="300" w:line="240" w:lineRule="auto"/>
    <w:contextualSpacing/>
  </w:pPr>
  <w:rPr>
    <w:rFonts w:asciiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia"
              w:hAnsiTheme="majorHAnsi" w:cstheme="majorBidi"
              w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
    <w:color w:val="17365D" w:themeColor="text2" w:themeShade="BF"/>
    <w:spacing w:val="5"/>
    <w:kern w:val="28"/>
    <w:sz w:val="52"/>
    <w:szCs w:val="52"/>
  </w:rPr>
</w:style>
  • 从中我们可以看到很多有趣的项目,但我们目前的重点只能限于 <w:rFonts>元素:
>>> title_style.element.rPr.rFonts.xml
<w:rFonts w:asciiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia"
          w:hAnsiTheme="majorHAnsi" w:cstheme="majorBidi"
          w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>

我们可以看到“Times New Roman”确实已应用于两种字体设置,但标题仍然出现在 Calibri 中,而这正是“majorHAnsi”所映射的内容。

要跳转到解决方案,如果我们设置 w:asciiTheme字体名称改为“Times New Roman”,标题根据需要显示:

from docx.oxml.ns import qn

rFonts = title_style.element.rPr.rFonts
rFonts.set(qn("w:asciiTheme"), "Times New Roman")

我希望同样的过程也适用于其他标题样式。

请注意,如果您要“从头开始”生成文档而不是编辑现有文档,那么从已具有所需样式的空白文档开始可能会更容易:

document = Document("my-starting-document.docx")

关于python - 如何更改 python-docx 中的标题字体和大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60921603/

相关文章:

javascript - Selenium Python Javascript 执行

python - pyenv 安装多个 python,但只识别一些

python - 按百分比获取随机 bool 值

python - 使用Python的docx库,如何对表格进行缩进?

python - 在python-docx中设置段落字体

python - 使用 python ('NoneType' 对象进行网页抓取没有属性 'get_text' )

perl - 使用 Perl(模块)创建 docx(Word)文档

PHP:在 Linux 中从 .PDF、.DOC 和 .DOCX 生成图像缩略图

command-line - Libreoffice 命令行在 Windows 7 上打开 DOCX 或转换为 PDF 时崩溃

python - 如何使用 python-docx 将样式应用于段落?