Fix page numbering: correct field insertion for PAGE and NUMPAGES
This commit is contained in:
+25
-25
@@ -345,44 +345,44 @@ def _setup_docx_style(doc, enable_line_numbering=False):
|
||||
|
||||
# PAGE field (current page number)
|
||||
run_page = p.add_run()
|
||||
r_page = run_page._r
|
||||
r_page.clear() # remove default empty run content
|
||||
r = run_page._r
|
||||
r.clear()
|
||||
|
||||
fldCharBegin = OxmlElement("w:fldChar")
|
||||
fldCharBegin.set(qn("w:fldCharType"), "begin")
|
||||
r_page.append(fldCharBegin)
|
||||
fldChar_begin = OxmlElement("w:fldChar")
|
||||
fldChar_begin.set(qn("w:fldCharType"), "begin")
|
||||
r.append(fldChar_begin)
|
||||
|
||||
instrTextPage = OxmlElement("w:instrText")
|
||||
instrTextPage.set(qn("xml:space"), "preserve")
|
||||
instrTextPage.text = "PAGE"
|
||||
r_page.append(instrTextPage)
|
||||
instrText = OxmlElement("w:instrText")
|
||||
instrText.set(qn("xml:space"), "preserve")
|
||||
instrText.text = "PAGE"
|
||||
r.append(instrText)
|
||||
|
||||
fldCharEnd = OxmlElement("w:fldChar")
|
||||
fldCharEnd.set(qn("w:fldCharType"), "end")
|
||||
r_page.append(fldCharEnd)
|
||||
fldChar_end = OxmlElement("w:fldChar")
|
||||
fldChar_end.set(qn("w:fldCharType"), "end")
|
||||
r.append(fldChar_end)
|
||||
|
||||
# Static text: " of "
|
||||
# " of " text
|
||||
run_of = p.add_run(" of ")
|
||||
run_of.font.name = "Courier"
|
||||
run_of.font.size = Pt(10)
|
||||
|
||||
# NUMPAGES field (total pages)
|
||||
run_numpages = p.add_run()
|
||||
r_numpages = run_numpages._r
|
||||
r_numpages.clear()
|
||||
r = run_numpages._r
|
||||
r.clear()
|
||||
|
||||
fldCharBegin2 = OxmlElement("w:fldChar")
|
||||
fldCharBegin2.set(qn("w:fldCharType"), "begin")
|
||||
r_numpages.append(fldCharBegin2)
|
||||
fldChar_begin = OxmlElement("w:fldChar")
|
||||
fldChar_begin.set(qn("w:fldCharType"), "begin")
|
||||
r.append(fldChar_begin)
|
||||
|
||||
instrTextNumpages = OxmlElement("w:instrText")
|
||||
instrTextNumpages.set(qn("xml:space"), "preserve")
|
||||
instrTextNumpages.text = "NUMPAGES"
|
||||
r_numpages.append(instrTextNumpages)
|
||||
instrText = OxmlElement("w:instrText")
|
||||
instrText.set(qn("xml:space"), "preserve")
|
||||
instrText.text = "NUMPAGES"
|
||||
r.append(instrText)
|
||||
|
||||
fldCharEnd2 = OxmlElement("w:fldChar")
|
||||
fldCharEnd2.set(qn("w:fldCharType"), "end")
|
||||
r_numpages.append(fldCharEnd2)
|
||||
fldChar_end = OxmlElement("w:fldChar")
|
||||
fldChar_end.set(qn("w:fldCharType"), "end")
|
||||
r.append(fldChar_end)
|
||||
|
||||
|
||||
def _add_cover_page(doc, doc_type, date, description):
|
||||
|
||||
Reference in New Issue
Block a user