携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第21天,点击查看活动详情
前言
Word 文档可以是几乎没有任何复杂样式的纯文本,但为了更好的理解文档内容,我们通常会为文档添加样式。Word 具有一组预定义的样式,可用于改变文档并突出显示文档的重要部分,我们也可以自定义样式以创建符合需求的文档。
Python 为 Word 文档添加样式
在本节中,我们将继续学习使用 python-docx 模块来处理 Word 文档,为文档添加样式。
首先,导入 python-docx 模块:
>>> import docx
利用 Document 类创建一个新文档对象 document:
>>> document = docx.Document()
添加一个包含多个部分 (run) 的段落,在 Word 中,一个段落可以包含多个 run,每个 run 都可能是段落中具有不同样式的较小部分。通常,当我们想要修改单个单词的相关样式我们通常将样式应用于 run,而影响整个段落的更改想要应用于段落对象。添加以不同方式(斜体、粗体和下划线等)突出显示段落中的某些单词:
>>> paragraph = document.add_paragraph('Deep learning has recently become a fast growing domain ')
>>> paragraph.add_run('and').bold = True
>>> paragraph.add_run(' is now used routinely for ')
<docx.text.run.Run object at 0x7f711bd89250>
>>> paragraph.add_run('classification').italic = True
>>> paragraph.add_run(' and ')
<docx.text.run.Run object at 0x7f711bd89310>
>>> paragraph.add_run('prediction').underline = True
>>> paragraph.add_run(' task.')
<docx.text.run.Run object at 0x7f711bd89e50>
创建多个段落并使用不同的默认样式设置这些段落的样式,例如 List Bullet、List Number 和 Quote 等:
>>> document.add_paragraph('availability of massive data;', style='List Bullet')
<docx.text.paragraph.Paragraph object at 0x7f711bd89250>
>>> document.add_paragraph('availability of efficient and affordable computing power;', style='List Bullet')
<docx.text.paragraph.Paragraph object at 0x7f711bd89310>
>>> document.add_paragraph('technical advances.', style='List Bullet')
<docx.text.paragraph.Paragraph object at 0x7f711bd89e10>
>>> document.add_paragraph('Introduction', style='List Number')
<docx.text.paragraph.Paragraph object at 0x7f711bd89250>
>>> document.add_paragraph('Method', style='List Number')
<docx.text.paragraph.Paragraph object at 0x7f711bd89e10>
>>> document.add_paragraph('Objective', style='List Number')
<docx.text.paragraph.Paragraph object at 0x7f711bd89250>
>>> document.add_paragraph('Representation', style='List Number')
<docx.text.paragraph.Paragraph object at 0x7f711bd89e10>
>>> document.add_paragraph('Architecture', style='List Number')
<docx.text.paragraph.Paragraph object at 0x7f711bd89310>
>>> document.add_paragraph('Challenge and Strategy', style='List Number')
<docx.text.paragraph.Paragraph object at 0x7f711bd89250>
>>> document.add_paragraph('Discussion and Conclusion', style='Quote')
<docx.text.paragraph.Paragraph object at 0x7f711bd89310>
默认情况下,每个 run 都是使用 Normal 样式创建的,bold、italic 或 underline 等属性都可以更改为 True 以设置 run 如何采用适当的样式或将这些样式进行组合;反之,当值为 False 时表示禁用这些属性;而值为 None 时将应用配置的默认值。
使用不同字体和字号创建不同的段落,例如,我们可以使用 Arial 字体并将字号设定为 28 磅,且段落使用右对齐 right:
>>> from docx.shared import Pt
>>> from docx.enum.text import WD_ALIGN_PARAGRAPH
>>> paragraph = document.add_paragraph('The representation is the nature and format of the information (data) used to train and to generate musical content.')
>>> paragraph.runs[0].font.name = 'Arial'
>>> paragraph.runs[0].font.size = Pt(28)
>>> paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
段落的对齐方式需要在段落对象中设置,其包含以下可选值:左对齐 (left)、右对齐 (right)、居中对齐 (center) 还是两端对齐 (justified)。
保存文档 word_style.docx:
>>> document.save('word_style.docx')
打开 word_style.docx 文档以验证其内容: