Fix page numbering: use correct python-docx field insertion for PAGE and NUMPAGES
Mirror and run GitLab CI / build (push) Has been cancelled
Ruff / ruff (push) Has been cancelled

This commit is contained in:
admin
2026-06-14 23:03:12 +00:00
parent 2f9299389b
commit 2e2bc3fb29
+13 -13
View File
@@ -343,46 +343,46 @@ def _setup_docx_style(doc, enable_line_numbering=False):
p = footer.paragraphs[0] p = footer.paragraphs[0]
p.alignment = WD_ALIGN_PARAGRAPH.RIGHT p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
# PAGE field # PAGE field (current page number)
run_page = p.add_run() run_page = p.add_run()
run_page.font.name = "Courier" r_page = run_page._r
run_page.font.size = Pt(10) r_page.clear() # remove default empty run content
fldCharBegin = OxmlElement("w:fldChar") fldCharBegin = OxmlElement("w:fldChar")
fldCharBegin.set(qn("w:fldCharType"), "begin") fldCharBegin.set(qn("w:fldCharType"), "begin")
run_page._r.addprevious(fldCharBegin) r_page.append(fldCharBegin)
instrTextPage = OxmlElement("w:instrText") instrTextPage = OxmlElement("w:instrText")
instrTextPage.set(qn("xml:space"), "preserve") instrTextPage.set(qn("xml:space"), "preserve")
instrTextPage.text = "PAGE" instrTextPage.text = "PAGE"
run_page._r.addprevious(instrTextPage) r_page.append(instrTextPage)
fldCharEnd = OxmlElement("w:fldChar") fldCharEnd = OxmlElement("w:fldChar")
fldCharEnd.set(qn("w:fldCharType"), "end") fldCharEnd.set(qn("w:fldCharType"), "end")
run_page._r.addprevious(fldCharEnd) r_page.append(fldCharEnd)
# " of " text # Static text: " of "
run_of = p.add_run(" of ") run_of = p.add_run(" of ")
run_of.font.name = "Courier" run_of.font.name = "Courier"
run_of.font.size = Pt(10) run_of.font.size = Pt(10)
# NUMPAGES field # NUMPAGES field (total pages)
run_numpages = p.add_run() run_numpages = p.add_run()
run_numpages.font.name = "Courier" r_numpages = run_numpages._r
run_numpages.font.size = Pt(10) r_numpages.clear()
fldCharBegin2 = OxmlElement("w:fldChar") fldCharBegin2 = OxmlElement("w:fldChar")
fldCharBegin2.set(qn("w:fldCharType"), "begin") fldCharBegin2.set(qn("w:fldCharType"), "begin")
run_numpages._r.addprevious(fldCharBegin2) r_numpages.append(fldCharBegin2)
instrTextNumpages = OxmlElement("w:instrText") instrTextNumpages = OxmlElement("w:instrText")
instrTextNumpages.set(qn("xml:space"), "preserve") instrTextNumpages.set(qn("xml:space"), "preserve")
instrTextNumpages.text = "NUMPAGES" instrTextNumpages.text = "NUMPAGES"
run_numpages._r.addprevious(instrTextNumpages) r_numpages.append(instrTextNumpages)
fldCharEnd2 = OxmlElement("w:fldChar") fldCharEnd2 = OxmlElement("w:fldChar")
fldCharEnd2.set(qn("w:fldCharType"), "end") fldCharEnd2.set(qn("w:fldCharType"), "end")
run_numpages._r.addprevious(fldCharEnd2) r_numpages.append(fldCharEnd2)
def _add_cover_page(doc, doc_type, date, description): def _add_cover_page(doc, doc_type, date, description):