设置页眉和页脚

from docx import Document
文件 = Document('c:/练习7.docx')
页眉 = 文件.sections[0].header # 获取第一个节的页眉
print(f'页眉中的段落数:{len(页眉.paragraphs)}')
段落 = 页眉.paragraphs[0] # 获取页眉的第一个段落
段落.add_run('这是第一节的页眉') # 添加页面内容

页脚 = 文件.sections[0].footer # 获取第一个节的页脚
段落 = 页脚.paragraphs[0] # 获取页脚的第一个段落
段落.add_run('这是第一节的页脚') # 添加页脚内容
文件.save('c:/test.docx')

设置新的节

from docx import Document
文件 = Document('c:/test.docx')

页眉 = 文件.sections[0].header # 获取第一个节的页眉
print(f'页眉中的段落数:{len(页眉.paragraphs)}')
段落 = 页眉.paragraphs[0] # 获取页眉的第一个段落
段落.add_run('这是第一节的页眉') # 添加页面内容

页脚 = 文件.sections[0].footer # 获取第一个节的页脚
段落 = 页脚.paragraphs[0] # 获取页脚的第一个段落
段落.add_run('这是第一节的页脚') # 添加页脚内容
文件.add_section() # 添加一个新的节
文件.add_section() # 添加第3个节

文件.save('c:/test.docx')

设置某个节和其他设置不同

from docx import Document
文件 = Document('c:/test.docx')

页眉 = 文件.sections[0].header # 获取第一个节的页眉
print(f'页眉中的段落数:{len(页眉.paragraphs)}')
段落 = 页眉.paragraphs[0] # 获取页眉的第一个段落
段落.add_run('这是第一节的页眉') # 添加页面内容

页脚 = 文件.sections[0].footer # 获取第一个节的页脚
段落 = 页脚.paragraphs[0] # 获取页脚的第一个段落
段落.add_run('这是第一节的页脚') # 添加页脚内容
文件.add_section() # 添加一个新的节
文件.add_section() # 添加第3个节

页眉 = 文件.sections[1].header # 获取第2个节的页眉
页眉.is_linked_to_previous = False # 不使用上节内容和样式

文件.save('c:/test.docx')

设置对齐

from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx import Document
文件 = Document('c:/test.docx')

页眉 = 文件.sections[1].header
页脚 = 文件.sections[1].footer

页眉.is_linked_to_previous = False # 获取第2个节的页眉,不使用上节内容和样式
页眉.paragraphs[0].add_run('这是第二节的页眉') # 获取页眉的第一个段落,加入文字
页眉.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER # 设置页眉居中对齐

页脚.is_linked_to_previous = False  # 获取第2个节的页脚,不使用上节内容和样式
页脚.paragraphs[0].add_run('这是第二节的页脚') # 获取页脚的第一个段落,加入文字
页脚.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER # 设置页脚居中对齐

文件.save('c:/test.docx')

设置页眉和页脚距离

from docx.shared import Cm # 导入单位转换
from docx import Document
文件 = Document('c:/test.docx')
文件.sections[1].header_distance = Cm(10) # 设置页眉为10厘米
文件.sections[1].footer_distance = Cm(10) # 设置页脚为10厘米
文件.save('c:/test.docx')

设置奇偶页不同

在python-docx中含有的偶数页页眉对象section.even_page_header,偶数页页脚对象section.even_page_footer;但却没有奇数页的对象,当设置的section.header或者section.footer时,默认设置在奇数页上。

from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt # 导入单位转换
from docx import Document
文件 = Document('c:/test.docx')
文件.settings.odd_and_even_pages_header_footer = True # 启动奇偶页不同

第一个节 = 文件.sections[0] # 第一个节
偶数页 = 第一个节.even_page_header # 获取偶数页
偶数页眉 = 偶数页.paragraphs[0].add_run('这是偶数页页眉')
偶数页眉.font.size = Pt(26) # 设置偶数页页眉大小
偶数页.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.RIGHT # 设置页眉右对齐

文件.save('c:/test.docx')

设置首页不同

from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx import Document
from docx.shared import Pt

文件 = Document('c:/test.docx')
第1个节 = 文件.sections[0] # 获取第1个节的对象
第1个节.different_first_page_header_footer = True # 设置启动首页不同

首页对象 = 第1个节.first_page_header  # 先要启动首页不同,才能设置对象
首页页眉 = 首页对象.paragraphs[0].add_run('这是首页')
首页页眉.font.size = Pt(48)

首页对象.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.LEFT
文件.save('c:/test.docx')