-
Notifications
You must be signed in to change notification settings - Fork 0
/
addpagenum.py
31 lines (25 loc) · 879 Bytes
/
addpagenum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import letter, A4
from pdfrw import PdfReader
from pdfrw.toreportlab import makerl
from pdfrw.buildxobj import pagexobj
import sys
import os
if len(sys.argv) != 2 or ".pdf" not in sys.argv[1].lower():
print(f"Usage: python {sys.argv[0]} <pdf filename>")
sys.exit()
input_file = sys.argv[1]
output_file = os.path.splitext(sys.argv[1])[0] + "_pgn.pdf"
reader = PdfReader(input_file)
pages = [pagexobj(p) for p in reader.pages]
canvas = Canvas(output_file)
for page_num, page in enumerate(pages, start=1):
canvas.doForm(makerl(canvas, page))
footer_text = f"{page_num}/{len(pages)}"
canvas.saveState()
canvas.setStrokeColorRGB(0, 0, 0)
canvas.setFont('Times-Roman', 14)
canvas.drawString(290, 10, footer_text)
canvas.restoreState()
canvas.showPage()
canvas.save()