>>> from env_helper import info; info()
页面更新时间: 2024-04-04 19:46:33
运行环境:
    Linux发行版本: Debian GNU/Linux 12 (bookworm)
    操作系统内核: Linux-6.1.0-18-amd64-x86_64-with-glibc2.36
    Python版本: 3.11.2

2.7. 处理文档的样式

文档的样式处理一般用于写入操作, Python-docx库里的样式主要有段落样式与字符样式,并且对列表与表格两种对象的样式有独立的处理方法。

2.7.1. 段落样式操作

我们可以选择操作打开本地文件或者新建一个工程,在这里我们就是使用本地的文件打开进行演示。 要注意标题是第一个段落。

使用段落样式

如果不知道Word段落样式是什么,可以输出查看一下。基本上,它允许一次将整个格式选项集应用于一个段落。如果了解CSS样式,则很像CSS样式。

先引入相关类库,并建立新文档:

>>> from docx import Document
>>> from docx.shared import Cm
>>> from docx.enum.text import WD_ALIGN_PARAGRAPH
>>> from docx.enum.style import WD_STYLE_TYPE
>>> from docx.shared import Inches , Pt, RGBColor , Length
>>> nfile = Document()

然后添加标题与段落:

>>> nfile.add_heading("demo1",0)
>>> nfile.add_paragraph('Lorem ipsum dolor sit amet.', style='List Bullet')
>>> nfile.add_paragraph('Lorem ipsum dolor sit amet.', style='List Bullet')
>>> nfile.add_paragraph('Lorem ipsum dolor sit amet.', style='List Bullet')
>>> nfile.add_paragraph('Lorem ipsum dolor sit amet.', style='List Bullet')
>>> nfile.add_paragraph('Lorem ipsum dolor sit amet.', style='List Bullet')
<docx.text.paragraph.Paragraph at 0x7f17a772fe10>

这种特殊的样式使段落显示为项目符号,使用非常方便。也可以在以后应用样式。这两行等效于上面的那一行:

>>> paragraph = nfile.add_paragraph('Lorem ipsum dolor sit amet.')
>>> paragraph.style = 'List Bullet'

在此示例中,使用样式名称“ List Bullet”指定样式。通常,样式名称与在Word用户界面(UI)中显示的名称完全相同。

设置段落缩进与对齐

将“First”首行缩进。其他向左向右缩进。

>>> paragraphs=nfile.paragraphs
>>> paragraphs[1].paragraph_format.first_line_indent = Inches(-0.25)
>>> paragraphs[3].paragraph_format.left_indent=Cm(0.74)
>>> paragraphs[5].paragraph_format.right_indent=Cm(2)

对齐方式

  • LEFT 左对齐

  • CENTER 文字居中

  • RIGHT 右对齐

  • JUSTIFY 文本两端对齐

与word软件内容的功能对应。对齐方式还有很多,我们以常用的为例:

>>> paragraphs[1].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.LEFT
>>> paragraphs[3].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.RIGHT
>>> paragraphs[4].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.CENTER
>>> paragraphs[1].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.JUSTIFY

设置段落间距

分为段前和段后 ,设置值用 Pt 单位是磅 ,如下:

>>> paragraphs[1].paragraph_format.space_before = Pt(18)
>>> paragraphs[3].paragraph_format.space_after = Pt(10)

设置段落行距

当行距为 最小值 和 固定值 时,设置值单位为 磅 ,需要用 Pt ;当行距为 多倍行距 时,设置值为数值,主要参数如下:

  • SINGLE 单倍行距(默认)

  • ONE_POINT_FIVE 1.5倍行距

  • DOUBLE2 倍行距

  • AT_LEAST 最小值

  • EXACTLY 固定值

  • MULTIPLE 多倍行距

>>> paragraphs[5].paragraph_format.line_spacing = Pt(18)
>>> paragraphs[3].paragraph_format.line_spacing = 1.5

设置段落分页

  • widow_control 孤行控制 防止在页面顶端单独打印段落末行或在页面底端单独打印段落首行。

  • keep_with_next 与下段同页 防止在选中段落与后面一段间插入分页符。

  • keep_together 段中不分页 防止在段落中出现分页符。

  • page_break_before 段前分页 在选中段落前插入分页符。

>>> paragraphs[3].paragraph_format.keep_with_next = True
>>> paragraphs[5].paragraph_format.page_break_before = True

2.7.2. 字符样式操作

可以操作文字的相关对象 主要有一下几个:

  • Paragraph 对象

  • ParagraphFormat 对象

  • Run 对象

  • Font 对象

  • TabStop 对象

  • TabStops 对象

添加 run 对象

为了了解粗体和斜体的工作原理,需要对段落中的内容有所了解。简短的版本是这样的:

段落包含所有块级格式,例如缩进,行高,制表符等。 在运行级别应用字符级别的格式,例如粗体和斜体 。段落中的所有内容都必须在一行中,但可以有多个。 因此,在中间有一个粗体字的段落将需要三个 run 对象,一个普通的 run 对象, 一个包含该单词的加粗 run 对象,以及后面的文本的另一个正常 run 对象。 通过向 .add_paragraph() 方法提供文本来添加段落时,该段落将一次运行。 您可以使用 .add_run() 以下段落中的方法添加更多内容:

>>> paragraph =nfile.add_paragraph('Lorem ipsum ')
>>> paragraph.add_run('dolor sit amet.')
<docx.text.run.Run at 0x7f17a7628f10>

这将产生一个看起来像是从单个字符串创建的段落。除非查看XML,否则不知道段落文本在何处拆分。请注意第一个字符串末尾的尾随空格。需要明确说明运行开始和结束处出现空格的位置。

Run对象具有 .bold.italic 属性,可以设置运行的值:

>>> paragraph = nfile.add_paragraph('Lorem ipsum ')
>>> run = paragraph.add_run('dolor')
>>> run.bold = True
>>> paragraph.add_run(' sit amet.')
<docx.text.run.Run at 0x7f1795150690>

也可以这样写。

>>> p = nfile.add_paragraph('A plain paragraph having some ')
>>> p.add_run('bold').bold = True
>>> p.add_run(' and some ')
>>> p.add_run('italic.').italic = True

使用.add_run() 方法时可以在结果上设置粗体或斜体:

>>> paragraph.add_run('dolor').bold = True
>>> run = paragraph.add_run('dolor')
>>> run.bold = True

应用字符样式

除了指定一组段落级别设置的段落样式外,Word还具有指定一组运行级别设置的字符样式。 通常可以将字符样式视为指定字体,包括字体,大小,颜色,粗体,斜体等。

与段落样式一样,在使用 Document() 调用打开的文档中必须已经定义了一种样式。

添加新的运行时可以指定字符样式:

>>> paragraph = nfile.add_paragraph('Normal text,字符样式  ')
>>> paragraph.add_run('text with emphasis.', 'Emphasis')
<docx.text.run.Run at 0x7f1795153850>

创建样式后,还可以将其应用于运行。这段代码产生与上面几行相同的结果:

>>> paragraph = nfile.add_paragraph('Normal text, ')
>>> run = paragraph.add_run('text with emphasis.')
>>> run.style = 'Emphasis'

与段落样式一样,样式名称与在Word UI中显示的一样。前面我们设置了字体的粗体和斜体,在官方 Font 类下还其他的相关设置。

设置字体属性

对 run 设置字体、大小、颜色下划线等,如下:

  • all_caps 全部大写字母

  • bold 加粗

  • color 字体颜色

  • complex_script 是否为“复杂代码”

  • cs_bold “复杂代码”加粗

  • cs_italic “复杂代码”斜体

  • double_strike 双删除线

  • emboss 文本以凸出页面的方式出现

  • hidden 隐藏

  • imprint 印记

  • italic 斜体

  • name 字体

  • no_proof 不验证语法错误

  • outline 显示字符的轮廓

  • shadow 阴影

  • small_caps 小型大写字母

  • snap_to_grid 定义文档网格时对齐网络

  • strike 删除线

  • subscript 下标

  • superscript 上标

  • underline 下划线

>>> nfile.save('./xx_style.docx')

2.7.3. 命名样式操作

更加合理的,是对文档的命名样式进行修改,而不是逐个修改。

>>> document=Document(r"./old/style.docx")
>>> styles = document.styles
>>> type(styles)
docx.styles.styles.Styles
>>> paragraph_styles = [s for s in styles if s.type == WD_STYLE_TYPE.PARAGRAPH]
>>>
>>> for style in paragraph_styles:
>>>     print(style.name , end = '; ' )
Normal; Header; Footer;
>>> tt = styles['Normal']
>>> tt.name
'Normal'
>>> tt.paragraph_format.left_indent = Cm(8)
>>> tt.font.bold = True
>>> tt.font.color.rgb = RGBColor(0x00, 0xff, 0xff)
>>> document.save('./xx_style2.docx')