Fix page numbering: use correct python-docx field insertion for PAGE and NUMPAGES
This commit is contained in:
+13
-13
@@ -343,46 +343,46 @@ def _setup_docx_style(doc, enable_line_numbering=False):
|
||||
p = footer.paragraphs[0]
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
|
||||
|
||||
# PAGE field
|
||||
# PAGE field (current page number)
|
||||
run_page = p.add_run()
|
||||
run_page.font.name = "Courier"
|
||||
run_page.font.size = Pt(10)
|
||||
r_page = run_page._r
|
||||
r_page.clear() # remove default empty run content
|
||||
|
||||
fldCharBegin = OxmlElement("w:fldChar")
|
||||
fldCharBegin.set(qn("w:fldCharType"), "begin")
|
||||
run_page._r.addprevious(fldCharBegin)
|
||||
r_page.append(fldCharBegin)
|
||||
|
||||
instrTextPage = OxmlElement("w:instrText")
|
||||
instrTextPage.set(qn("xml:space"), "preserve")
|
||||
instrTextPage.text = "PAGE"
|
||||
run_page._r.addprevious(instrTextPage)
|
||||
r_page.append(instrTextPage)
|
||||
|
||||
fldCharEnd = OxmlElement("w:fldChar")
|
||||
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.font.name = "Courier"
|
||||
run_of.font.size = Pt(10)
|
||||
|
||||
# NUMPAGES field
|
||||
# NUMPAGES field (total pages)
|
||||
run_numpages = p.add_run()
|
||||
run_numpages.font.name = "Courier"
|
||||
run_numpages.font.size = Pt(10)
|
||||
r_numpages = run_numpages._r
|
||||
r_numpages.clear()
|
||||
|
||||
fldCharBegin2 = OxmlElement("w:fldChar")
|
||||
fldCharBegin2.set(qn("w:fldCharType"), "begin")
|
||||
run_numpages._r.addprevious(fldCharBegin2)
|
||||
r_numpages.append(fldCharBegin2)
|
||||
|
||||
instrTextNumpages = OxmlElement("w:instrText")
|
||||
instrTextNumpages.set(qn("xml:space"), "preserve")
|
||||
instrTextNumpages.text = "NUMPAGES"
|
||||
run_numpages._r.addprevious(instrTextNumpages)
|
||||
r_numpages.append(instrTextNumpages)
|
||||
|
||||
fldCharEnd2 = OxmlElement("w:fldChar")
|
||||
fldCharEnd2.set(qn("w:fldCharType"), "end")
|
||||
run_numpages._r.addprevious(fldCharEnd2)
|
||||
r_numpages.append(fldCharEnd2)
|
||||
|
||||
|
||||
def _add_cover_page(doc, doc_type, date, description):
|
||||
|
||||
Reference in New Issue
Block a user