Add page numbers to footer: 'X of Y' (bottom left)
- Use PAGE and NUMPAGES fields for dynamic page numbering. - Footer aligned left.
This commit is contained in:
@@ -551,6 +551,39 @@ def create_transcript_docx(text: str, filename: str):
|
||||
line_number += 1
|
||||
_add_transcript_paragraph(doc, cl, line_number=line_number)
|
||||
|
||||
# Add page numbers to footer: "X of Y" (bottom left)
|
||||
section = doc.sections[0]
|
||||
footer = section.footer
|
||||
footer.is_linked_to_previous = False
|
||||
footer_para = footer.paragraphs[0] if footer.paragraphs else footer.add_paragraph()
|
||||
footer_para.alignment = WD_ALIGN_PARAGRAPH.LEFT
|
||||
|
||||
# Clear any existing content
|
||||
for r in footer_para.runs:
|
||||
r.text = ""
|
||||
|
||||
def add_field(run, code):
|
||||
fldChar = OxmlElement("w:fldChar")
|
||||
fldChar.set(qn("w:fldCharType"), "begin")
|
||||
run._r.append(fldChar)
|
||||
|
||||
instrText = OxmlElement("w:instrText")
|
||||
instrText.set(qn("xml:space"), "preserve")
|
||||
instrText.text = code
|
||||
run._r.append(instrText)
|
||||
|
||||
fldCharEnd = OxmlElement("w:fldChar")
|
||||
fldCharEnd.set(qn("w:fldCharType"), "end")
|
||||
run._r.append(fldCharEnd)
|
||||
|
||||
run_page = footer_para.add_run()
|
||||
add_field(run_page, " PAGE ")
|
||||
|
||||
run_of = footer_para.add_run(" of ")
|
||||
|
||||
run_total = footer_para.add_run()
|
||||
add_field(run_total, " NUMPAGES ")
|
||||
|
||||
# Save
|
||||
doc.save(filename)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user