docx: header/footer content lost across sections in multi-section documents
Bug
MsWordDocumentBackend._add_header_footer silently drops header/footer content in multi-section .docx files, in two separate ways:
- Sections after the first are skipped entirely unless they set
different_first_page_header_footer. A later section's own distinct (non-inherited) header/footer is never parsed. - When
different_first_page_header_footeris set on a section, only the first-page header/footer is parsed. The regular header/footer used on every page after the first is dropped.
Both are silent data loss: no error or warning is raised, the content just never appears in the resulting DoclingDocument.
Steps to reproduce
import docx
from docx.enum.section import WD_SECTION
from docling.document_converter import DocumentConverter
d = docx.Document()
d.sections[0].header.paragraphs[0].text = "Header of SECTION 1"
d.add_paragraph("page 1 body")
d.add_section(WD_SECTION.NEW_PAGE)
s2 = d.sections[1]
s2.header.is_linked_to_previous = False
s2.header.paragraphs[0].text = "Header of SECTION 2"
d.add_paragraph("page 2 body")
d.save("repro.docx")
doc = DocumentConverter().convert("repro.docx").document
headers = [
child.resolve(doc).text
for group in doc.groups
if group.name == "page header"
for child in group.children
]
print(headers)
# actual: ['Header of SECTION 1']
# expected: ['Header of SECTION 1', 'Header of SECTION 2']
A second, independent repro for the different_first_page_header_footer case:
import docx
from docling.document_converter import DocumentConverter
d = docx.Document()
s0 = d.sections[0]
s0.different_first_page_header_footer = True
s0.header.paragraphs[0].text = "REGULAR HEADER"
s0.first_page_header.paragraphs[0].text = "FIRST PAGE HEADER"
d.save("repro2.docx")
doc = DocumentConverter().convert("repro2.docx").document
headers = [
child.resolve(doc).text
for group in doc.groups
if group.name == "page header"
for child in group.children
]
print(headers)
# actual: ['FIRST PAGE HEADER']
# expected: ['FIRST PAGE HEADER', 'REGULAR HEADER']
Docling version
2.112.0
Python version
3.13.6
Thanks for the detailed report and repro scripts, @DanielNg0729. I can confirm both issues in the current implementation.
The root cause is in _add_header_footer — specifically these two code paths:
Bug 1 — Later sections skipped:
if sec_idx > 0 and not section.different_first_page_header_footer:
continue
Any section after the first is entirely skipped unless different_first_page_header_footer is set, so a section with its own distinct (non-inherited) regular header/footer is never parsed [1].
Bug 2 — Regular header/footer dropped when first-page flag is set:
hdr = (
section.first_page_header
if section.different_first_page_header_footer
else section.header
)
When different_first_page_header_footer is True, only first_page_header / first_page_footer is selected — the regular header / footer (used on every page after the first) is never parsed [2].
A fix would need to:
- Remove the
continueguard (or replace it with a check for whether the section's header/footer is actually linked to the previous section, e.g. viaheader.is_linked_to_previous). - When
different_first_page_header_footerisTrue, parse both the first-page and the regular header/footer rather than picking one or the other.
There's also a related open issue about non-repeating header/footer misclassification (#3693) that touches on adjacent header/footer handling concerns.
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.