from docx import Document from docx.shared import Pt from docx.enum.text import WD_ALIGN_PARAGRAPH #导入对齐库 文件=Document(r'E:word练习页眉页脚.docx') 第一节=文件.sections[0] 第一节.different_first_page_header_footer=True #设置首页不同 首页对象=第一节.first_page_header 页眉=首页对象.paragraphs[0].add_run('这是页眉') 页眉.font.size=Pt(12) 首页对象.paragraphs[0].alignment=WD_ALIGN_PARAGRAPH.CENTER 首页对象=第一节.first_page_footer 页脚=首页对象.paragraphs[0].add_run('这是页脚') 文件.save(r'E:word练习首页不同.docx')