页眉和页脚顶部和底部边距

Header and footer top and bottom margin

我有一个生成 docx 文件的 s 脚本,除了页眉和页脚未与顶部和底部对齐外,一切正常。

我有

        mydoc = docx.Document()
        section_g = mydoc.sections
        section_h = mydoc.sections[0]
        header = section_h.header
        header_para = header.paragraphs[0]
        footer = section_h.footer
        footer_para = footer.paragraphs[0]


        for section in section_g:
            section.top_margin = Cm(0)
            section.bottom_margin = Cm(0)
            section.left_margin = Cm(0)
            section.right_margin = Cm(0)

这对文档的其余部分非常有用,但页眉和页脚的顶部和左边距不是 0。

我错过了什么?谢谢

在此处查看 Section.header_distance.footer_distance 的 API 文档:https://python-docx.readthedocs.io/en/latest/api/section.html#docx.section.Section.header_distance

我想这就是您想要的设置。基本上它分别确定页面顶部或底部与页眉或页脚之间的 space。通过结合顶部和底部边距值调整这些值,您可以调整页眉和页脚的开始位置以及“正文”文本开始和结束的垂直位置。