首页 > 解决方案 > 如何使用 python-docx 在 docx 中设置页脚

问题描述

我无法在 Docx 中使用python-docx.

我有这样的代码:

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

document = Document()
document.add_heading('Document Title', 0)
section = document.sections[0]
footer = section.footer
footer.add_paragraph("This is page number")
footer.footer_distance = Cm(3)
footer.bottom_margin = Cm(5.0)
footer.alignment = WD_ALIGN_PARAGRAPH.RIGHT

document.save('mydoc.docx')

我将页脚设置为bottom = Cm(5.0), footer_distance = Cm(3), 对齐RIGHT。但什么也没有发生。

有人可以帮助我吗?也许我错过了什么?谢谢。

标签: python-3.xpython-docx

解决方案


页脚对象没有.footer_distance.bottom_margin属性。这些在该部分。
https://python-docx.readthedocs.io/en/latest/api/section.html#docx.section.Section.footer_distance

如果您将代码更改为:

section.footer_distance = Cm(3)

您应该看到文档中的间距发生了变化。


推荐阅读