Created
May 7, 2024 17:18
-
-
Save oclero/793be82a0eafe969b662c8d05307056d to your computer and use it in GitHub Desktop.
Add watermark to a PDF file with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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