Skip to content

Instantly share code, notes, and snippets.

@meksor
Created March 21, 2023 14:18
Show Gist options
  • Save meksor/e9fc564765ee54e357a5b0d571aa112e to your computer and use it in GitHub Desktop.
Save meksor/e9fc564765ee54e357a5b0d571aa112e to your computer and use it in GitHub Desktop.
import pypdf
import glob
from pathlib import Path
files = "2022/*/*.pdf"
with open("Unterschrift.pdf", "rb") as overlay:
for file in glob.glob(files):
with open(file, "rb") as inFile:
original = pypdf.PdfReader(inFile)
background = original.pages[0]
foreground = pypdf.PdfReader(overlay).pages[0]
# merge the first two pages
background.merge_page(foreground)
# add all pages to a writer
writer = pypdf.PdfWriter()
for page in original.pages:
writer.add_page(page)
# write everything in the writer to a file
outpath = "out/" + file
Path(outpath).parent.mkdir(exist_ok=True, parents=True)
with open(outpath, "wb") as outFile:
writer.write(outFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment