需求

得到一批文件,文件集的页眉页脚不是自己想要的,手动一个个删比较慢,好几百个文件,需要些一个批量删除并添加新的脚本,先只分享一个删的,有需要新增的可留言

解决

from docx import Document
 
def remove_headers_and_footers(docx_path):
    doc = Document(docx_path)
    for section in doc.sections:
        # 删除页眉和页脚
        section.different_first_page_header_footer = False
        section.header.is_linked_to_previous = True
        section.footer.is_linked_to_previous = True
    doc.save(docx_path)
 
docx_path = "test.docx"  # 替换为您的 Word 文件路径
remove_headers_and_footers(docx_path)

求助

有不一样或更好的方案,欢迎留言,成长你我他

更多推荐