Skip to content

Instantly share code, notes, and snippets.

@oclero
Created May 7, 2024 17:18
Show Gist options
  • Select an option

  • Save oclero/793be82a0eafe969b662c8d05307056d to your computer and use it in GitHub Desktop.

Select an option

Save oclero/793be82a0eafe969b662c8d05307056d to your computer and use it in GitHub Desktop.
Add watermark to a PDF file with Python
import os
import sys
import getopt
import glob
import os.path
import fnmatch
from os import listdir
from os.path import isfile, join
from PyPDF2 import PdfFileReader, PdfFileWriter
# Main.
if __name__ == '__main__':
watermarkPath = os.path.abspath('watermark.pdf')
inputPath = 'input.pdf'
with open(watermarkPath, "rb") as watermarkFile:
watermark = PdfFileReader(watermarkFile)
watermarkFirstPage = watermark.getPage(0)
outputPdf = PdfFileWriter()
inputPdf = PdfFileReader(inputPath)
pageCount = inputPdf.getNumPages()
for i in range(pageCount):
page = inputPdf.getPage(i)
page.mergePage(watermarkFirstPage)
outputPdf.addPage(page)
outputPath = os.path.abspath('output.pdf')
with open(outputPath, 'wb') as outputFile:
outputPdf.write(outputFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment