1. 分区
就是分多个节(部分),每个节可以单独设置页面等信息。
from docx import Document
doc = Document()
# 获取Sections对象
sections = doc.sections
print(type(sections)) # <class 'docx.section.Sections'>
# 获取第一个节:Section
print(type(sections[0])) # <class 'docx.section.Section'>
# 一共有几个节
print(len(sections)) # 1
1.1 添加新的节
from docx import Document
doc = Document()
# 添加一个新节
doc.add_section()
print(len(doc.sections))
添加时可以指定起始的类型:
from docx import Document
from docx.enum.section import WD_SECTION_START
doc = Document()
# 添加一个新节
doc.add_section(start_type=WD_SECTION_START.NEW_PAGE)
# 可以用-1访问最后一个节
print(doc.sections[-1].start_type)
print(len(doc.sections))
- CONTINUOUS:连续分隔符
- NEW_PAGE:新页的分隔符
- EVEN_PAGE:偶数页的分隔符
- ODD_PAGE:奇数页的分隔符
word里分节符奇数页和偶数页没有区别,都是插入不连续的分节符,只是奇数页分节符插入位置的页面的页码为奇数,而偶数页分节符插入位置的页面页码为偶数。一般用于文档装订成册,所有新的章节都要从偶数、奇数页开始,然后每个章节的页眉页脚都有不同,这个就可以使用这种类型的分节符了,一般情况下用的不多。
2. 页面设置
2.1 页面宽和高
# 知识点:
# section.page_width 页宽
# section.page_height 页高
from docx import Document
from docx.enum.section import WD_SECTION_START
from docx.shared import Cm
doc = Document()
doc.add_section(start_type=WD_SECTION_START.NEW_PAGE)
section = doc.sections[-1]
# 页面大小
# 宽
section.page_width = Cm(10)
# 高
section.page_height = Cm(10)
2.2 页面方向
# 知识点:
# section.orientation = 方向
# - WD_ORIENTATION.LANDSCAPE 横向
# - WD_ORIENTATION.PORTRAIT 纵向
from docx import Document
from docx.enum.section import , WD_ORIENTATION
doc = Document()
section = doc.sections[0]
print(section.orientation) # PORTRAIT (0)
# 改为横向
width, height = section.page_width, section.page_height
# 设置为横向
section.orientation = WD_ORIENTATION.LANDSCAPE
# 宽和高调整
section.page_width = height
section.page_height = width
print(section.orientation)
2.3 页边距
# 左边界,右边界
# - section.left_margin
# - section.right_margin
# 上这界,下边界
# - section.top_margin
# - section.bottom_margin
from docx import Document
from docx.shared import Cm
doc = Document()
section = doc.sections[0]
# 边距
section.left_margin = Cm(1.5)
section.right_margin = Cm(1.5)
section.top_margin = Cm(1.5)
section.bottom_margin = Cm(1.5)
doc.save("hello.docx")
2.4 装订线
# 装订线为1厘米
section.gutter=Cm(1)
3. 页眉与页脚
3.1 页眉
s1 = doc.sections[0]
# 获取第一节的页眉
header = s1.header
print(type(header)) # <class 'docx.section._Header'>
# 添加文字
# header.add_paragraph("hello")
p = header.paragraphs[0]
r = p.add_run("页眉")
r.font.color.rgb = RGBColor(255, 0, 0)
p.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
断开链接:页眉是自动与前一条相同的,节与节之间如果需要不同,则需要断开链接。
header = s2.header
# 断开与上一条的链接
header.is_linked_to_previous = False
p = header.paragraphs[0]
r = p.add_run("页眉2")
r.font.color.rgb = RGBColor(255, 0, 0)
p.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
页眉与顶端的距离:
from docx import Document
from docx.shared import Cm
doc = Document()
doc.add_section()
doc.sections[0].header.paragraphs[0].text = "我是页眉"
doc.sections[0].header_distance = Cm(0.5)
doc.save("hello.docx")
奇偶页不同:
from docx import Document
from docx.shared import Cm
doc = Document()
# 设置奇偶页不同
doc.settings.odd_and_even_pages_header_footer = True
doc.sections[0].header.paragraphs[0].text = "我是奇数页眉"
doc.sections[0].header_distance = Cm(0.5)
# 偶数页眉用:even_page_header表示
doc.sections[0].even_page_header.paragraphs[0].text = "我是偶数页眉"
doc.add_page_break()
首页不同:
from docx import Document
from docx.shared import Cm
doc = Document()
# 首页不同
doc.sections[0].different_first_page_header_footer = True
# 设置首页的页眉
doc.sections[0].first_page_header.paragraphs[0].text="我是首页"
# 设置普通页的页眉
doc.sections[0].header.paragraphs[0].text = "我是普通页"
doc.sections[0].header_distance = Cm(0.5)
doc.add_page_break()
doc.add_page_break()
doc.save("hello.docx")
3.2 页脚
与页眉对比应用:
- 页眉与页脚对象:
doc.sections[0].header
doc.sections[0].footer
- 页眉与页脚链接上一节:
header.is_linked_to_previous
footer.is_linked_to_previous
- 页眉与页脚与顶端和底顶的距离:
section.header_distance
section.footer_distance
- 页眉与页脚奇偶页不同:
先开启:document.settings.odd_and_even_pages_header_footer=True
- section.even_page_header
- section.even_page_footer
- 首页不同
先开启:section.different_first_page_header_footer = True
-
section.first_page_header
页眉对象 -
section.first_page_footer
页脚对象