gpt4 book ai didi

python - 使用 python-docx 创建一个文档,其中标题仅出现在第一页上

转载 作者:太空宇宙 更新时间:2023-11-03 20:04:30 24 4
gpt4 key购买 nike

我正在使用 python-docx 从头开始​​创建一个 Word 文档,并且我已成功向该文档添加了图像标题。然而,标题图像在所有页面上重复,我不完全理解 sectionsheader.is_linked_to_previous 如何工作以弄清楚如何只显示标题第一页。

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 - 使用 python-docx 创建一个文档,其中标题仅出现在第一页上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59047358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com