我想在生成的文档的每个部分都有不同的页脚。 但是,修改一个部分中的页脚似乎会修改所有部分中的页脚。 使用“first_page_footer”属性似乎没有帮助。 下面的最小示例说明了我的问题:
from docx import Document
from docx.enum.section import WD_SECTION
document = Document()
n_pages = 5
for pp in range(0, n_pages):
if pp < n_pages - 1:
document.add_section(WD_SECTION.NEW_PAGE)
current_section = document.sections[-1]
current_section.footer.paragraphs[0].text = str(pp)
current_section.first_page_footer.paragraphs[0].text = str(pp)
document.save('Apple.doc')
# All footers contain the number 4
document.sections[1].footer.paragraphs[0].text = 'Banana'
document.save('Banana.doc')
# All footers contain the word Banana
linked_to_previous属性需要设置为False(否则,更改一个部分中的页脚将更改为所有先前的页脚)。下面是更正后的示例。
from docx import Document
from docx.enum.section import WD_SECTION
document = Document()
n_pages = 5
for pp in range(0, n_pages):
current_section = document.sections[-1]
current_section.footer.is_linked_to_previous = False
current_section.footer.paragraphs[0].text = str(pp)
if pp < n_pages - 1:
document.add_section(WD_SECTION.NEW_PAGE)
document.save('Apple.doc')
#Footers contain number 0, 1, 2,3 ,4
document.sections[1].footer.paragraphs[0].text = 'Banana'
document.save('Banana.doc')
# Only the second page has the word Banana