Outil d'imposition PDF desktop (PyQt6) remplacant Quite Imposing Plus. 9 fonctions : livret, n-up, step & repeat, imposition manuelle, jointure pages, pages blanches, numerotation, masquage, fonds perdus. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
90 lines
2.5 KiB
Python
90 lines
2.5 KiB
Python
"""Moteur Numerotation de pages."""
|
|
|
|
from pypdf import PdfReader, PdfWriter
|
|
from reportlab.pdfgen import canvas
|
|
from reportlab.lib.units import mm
|
|
from .pdf_utils import get_page_size
|
|
import io
|
|
|
|
|
|
POSITIONS = {
|
|
"haut-gauche": ("left", "top"),
|
|
"haut-centre": ("center", "top"),
|
|
"haut-droite": ("right", "top"),
|
|
"milieu-gauche": ("left", "middle"),
|
|
"milieu-centre": ("center", "middle"),
|
|
"milieu-droite": ("right", "middle"),
|
|
"bas-gauche": ("left", "bottom"),
|
|
"bas-centre": ("center", "bottom"),
|
|
"bas-droite": ("right", "bottom"),
|
|
}
|
|
|
|
|
|
def add_page_numbers(input_path, output_path, position="bas-centre",
|
|
font_name="Helvetica", font_size=10, color=(0, 0, 0),
|
|
prefix="", suffix="", start_number=1,
|
|
margin_x=10*mm, margin_y=10*mm,
|
|
apply_to="all"):
|
|
"""Ajouter des numeros de page.
|
|
|
|
position: cle de POSITIONS
|
|
apply_to: "all", "even", "odd"
|
|
"""
|
|
reader = PdfReader(input_path)
|
|
writer = PdfWriter()
|
|
|
|
h_align, v_align = POSITIONS.get(position, ("center", "bottom"))
|
|
total_pages = len(reader.pages)
|
|
|
|
for i, page in enumerate(reader.pages):
|
|
page_num = start_number + i
|
|
|
|
# Filtre pair/impair
|
|
if apply_to == "even" and page_num % 2 != 0:
|
|
writer.add_page(page)
|
|
continue
|
|
if apply_to == "odd" and page_num % 2 == 0:
|
|
writer.add_page(page)
|
|
continue
|
|
|
|
w, h = get_page_size(page)
|
|
|
|
# Generer le texte
|
|
text = f"{prefix}{page_num}{suffix}"
|
|
|
|
# Creer overlay avec le numero
|
|
packet = io.BytesIO()
|
|
c = canvas.Canvas(packet, pagesize=(w, h))
|
|
c.setFont(font_name, font_size)
|
|
c.setFillColorRGB(*color)
|
|
|
|
# Calculer la position
|
|
text_width = c.stringWidth(text, font_name, font_size)
|
|
|
|
if h_align == "left":
|
|
x = margin_x
|
|
elif h_align == "right":
|
|
x = w - margin_x - text_width
|
|
else:
|
|
x = (w - text_width) / 2
|
|
|
|
if v_align == "top":
|
|
y = h - margin_y - font_size
|
|
elif v_align == "middle":
|
|
y = (h - font_size) / 2
|
|
else:
|
|
y = margin_y
|
|
|
|
c.drawString(x, y, text)
|
|
c.showPage()
|
|
c.save()
|
|
|
|
packet.seek(0)
|
|
overlay_reader = PdfReader(packet)
|
|
page.merge_page(overlay_reader.pages[0])
|
|
writer.add_page(page)
|
|
|
|
with open(output_path, "wb") as f:
|
|
writer.write(f)
|
|
return output_path
|