Add 'Page X of Y' footer to all .docx files
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 21:51:12 +00:00
parent 2a2a5e024c
commit 6c11a8f19a
+47
View File
@@ -22,6 +22,7 @@ from docx import Document
from docx.shared import Inches, Pt from docx.shared import Inches, Pt
from docx.oxml.ns import qn from docx.oxml.ns import qn
from docx.oxml import OxmlElement from docx.oxml import OxmlElement
from docx.enum.text import WD_ALIGN_PARAGRAPH
logger = logging.getLogger("scraibe.email_sender") logger = logging.getLogger("scraibe.email_sender")
@@ -304,6 +305,52 @@ def _setup_docx_style(doc):
font.name = "Courier" font.name = "Courier"
font.size = Pt(12) font.size = Pt(12)
# Add "Page X of Y" footer, right-aligned
footer = section.footer
footer.is_linked_to_previous = False
p = footer.paragraphs[0]
p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
run = p.add_run()
run.font.name = "Courier"
run.font.size = Pt(10)
# Field: PAGE
fldChar1 = OxmlElement("w:fldChar")
fldChar1.set(qn("w:fldCharType"), "begin")
run._r.addprevious(fldChar1)
instrText = OxmlElement("w:instrText")
instrText.set(qn("xml:space"), "preserve")
instrText.text = " PAGE "
run._r.addprevious(instrText)
fldChar2 = OxmlElement("w:fldChar")
fldChar2.set(qn("w:fldCharType"), "end")
run._r.addprevious(fldChar2)
# Static text: " of "
run_of = p.add_run(" of ")
run_of.font.name = "Courier"
run_of.font.size = Pt(10)
# Field: NUMPAGES
run2 = p.add_run()
run2.font.name = "Courier"
run2.font.size = Pt(10)
fldChar3 = OxmlElement("w:fldChar")
fldChar3.set(qn("w:fldCharType"), "begin")
run2._r.addprevious(fldChar3)
instrText2 = OxmlElement("w:instrText")
instrText2.set(qn("xml:space"), "preserve")
instrText2.text = " NUMPAGES "
run2._r.addprevious(instrText2)
fldChar4 = OxmlElement("w:fldChar")
fldChar4.set(qn("w:fldCharType"), "end")
run2._r.addprevious(fldChar4)
def _add_cover_page(doc, doc_type, date, description): def _add_cover_page(doc, doc_type, date, description):
p_type = doc.add_paragraph() p_type = doc.add_paragraph()