使用 python-docx 创建文档,其中 header 仅出现在第一页
Create a document with python-docx where the header only appears on the first page
我正在使用 python-docx 从头开始创建一个 word 文档,并且我已经设法将图像 header 添加到文档中。然而 header 图像在所有页面上重复出现,我不完全理解 sections
和 header.is_linked_to_previous
是如何工作的,无法弄清楚如何只在 header第一页。
document = Document()
header = document.sections[0].header
htable = header.add_table(1, 1, Inches(6))
htab_cells = htable.rows[0].cells
ht0 = htab_cells[0].add_paragraph()
kh = ht0.add_run()
kh.add_picture('BothHeaderLogos.png', width=Inches(5.6))
current_section = document.sections[-1]
new_section = document.add_section(WD_SECTION.ODD_PAGE)
document.add_heading("Sequence Summary", level=1)
header.is_linked_to_previous = False
我想你要找的是document.sections[0].first_page_header
。此处定义的页眉仅出现在该部分的第一页上。
.is_linked_to_previous
属性 仅当您已经有多个部分时才需要。创建第二个部分只是为了添加那是不必要的。
页眉和页脚记录在此处:
https://python-docx.readthedocs.io/en/latest/user/hdrftr.html
这些电话的 API 详细信息在此处:
https://python-docx.readthedocs.io/en/latest/api/section.html
我正在使用 python-docx 从头开始创建一个 word 文档,并且我已经设法将图像 header 添加到文档中。然而 header 图像在所有页面上重复出现,我不完全理解 sections
和 header.is_linked_to_previous
是如何工作的,无法弄清楚如何只在 header第一页。
document = Document()
header = document.sections[0].header
htable = header.add_table(1, 1, Inches(6))
htab_cells = htable.rows[0].cells
ht0 = htab_cells[0].add_paragraph()
kh = ht0.add_run()
kh.add_picture('BothHeaderLogos.png', width=Inches(5.6))
current_section = document.sections[-1]
new_section = document.add_section(WD_SECTION.ODD_PAGE)
document.add_heading("Sequence Summary", level=1)
header.is_linked_to_previous = False
我想你要找的是document.sections[0].first_page_header
。此处定义的页眉仅出现在该部分的第一页上。
.is_linked_to_previous
属性 仅当您已经有多个部分时才需要。创建第二个部分只是为了添加那是不必要的。
页眉和页脚记录在此处:
https://python-docx.readthedocs.io/en/latest/user/hdrftr.html
这些电话的 API 详细信息在此处:
https://python-docx.readthedocs.io/en/latest/api/section.html