'How to add watermark in all pages of PDF files with python?
I'm try to adding watermark to every pages of my PDF file.My PDF files have 58 pages but my output file has get only last page in my PDF file.
This's my code:
from PyPDF2 import PdfFileReader, PdfFileWriter
watermark_pdf = PdfFileReader("watermark.pdf")
watermark_page = watermark_pdf.getPage(0)
reader = PdfFileReader("original_document.pdf")
for page in reader.pages:
page.mergePage(watermark_page)
output = PdfFileWriter()
output.addPage(page)
with open("watermarked_document.pdf", "wb") as fp:
output.write(fp)
Please tell me how to add watermark all pages.
Solution 1:[1]
You're rewriting your "merged" file for each page.
Try something like
from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter
pdf_file = "C:/Users/11359023/Desktop/deepfake_vee.pdf"
watermark = "C:/Users/11359023/Desktop/simple.pdf"
merged = "C:/Users/11359023/Desktop/merged.pdf"
with open(pdf_file, "rb") as input_file, open(watermark, "rb") as watermark_file:
input_pdf = PdfFileReader(input_file)
watermark_pdf = PdfFileReader(watermark_file)
watermark_page = watermark_pdf.getPage(0)
output = PdfFileWriter()
for i in range(input_pdf.getNumPages()):
pdf_page = input_pdf.getPage(i)
pdf_page.mergePage(watermark_page)
output.addPage(pdf_page)
with open(merged, "wb") as merged_file:
output.write(merged_file)
instead.
Solution 2:[2]
My code is taking in multiple pdf files passed in as cmd promt arguments , merges them together to super.pdf and then adding watermark to each page in super.pdf after what saves it as watermarked.pdf
import PyPDF2
import sys
pdf_file_list = sys.argv[1:]
watermark = 'wtr.pdf'
merger = PyPDF2.PdfFileMerger()
writer = PyPDF2.PdfFileWriter()
for file in pdf_file_list:
merger.append(file)
merger.write('super.pdf')
with open('super.pdf', 'rb') as fileinput:
pdf_file = PyPDF2.PdfFileReader(fileinput)
with open(watermark, 'rb') as filewatermark:
watermark_pdf = PyPDF2.PdfFileReader(filewatermark)
for page in range(pdf_file.getNumPages()):
current_pdf_page = pdf_file.getPage(page)
first_page_watermark = watermark_pdf.getPage(0)
current_pdf_page.mergePage(first_page_watermark)
writer.addPage(current_pdf_page)
with open("watermarked.pdf", 'wb')as file_output:
writer.write(file_output)
Solution 3:[3]
import PyPDF2
template = PyPDF2.PdfFileReader(open('C:/Users/11359023/Desktop/deepfake_vee.pdf', 'rb'))
watermark = PyPDF2.PdfFileReader(open('C:/Users/11359023/Desktop/simple.pdf', 'rb'))
output = PyPDF2.PdfFileWriter()
for i in range(template.getNumPages()):
page = template.getPage(i)
page.mergePage(watermark.getPage(0))
output.addPage(page)
with open('C:/Users/11359023/Desktop/merged.pdf', 'wb') as file:
output.write(file)
Solution 4:[4]
or you can use this if you have a folder containing many pdfs and wanna add the watermark to all of them and save them to new files *assuming the file with watermark is named wtr.pdf containing the watermark on the first page:
import PyPDF2
import pathlib
path = 'D:\Python\PDF processing' #add your folder path here
folder = pathlib.Path(path).resolve()
for item in folder.iterdir():
if item.is_file() and item.suffix == '.pdf' and not item.name == 'wtr.pdf':
with open(f'{item}', 'rb') as file, open('wtr.pdf', 'rb') as wtr:
input_file_reader = PyPDF2.PdfFileReader(file) # input file reader
wtr_reader = PyPDF2.PdfFileReader(wtr) # watermark reader
wtr_page = wtr_reader.getPage(0) # getting the page with the watermark
output = PyPDF2.PdfFileWriter() # the output writer
for i in range(input_file_reader.numPages): # looping the input reader pages
page = input_file_reader.getPage(i) # getting pages one by one
page.mergePage(wtr_page) # merging each page with the watermark
output.addPage(page) # adding the merged page to the output writer
with open(f'merged_{item.name}', 'wb') as merged_file:
output.write(merged_file) # saving to a file from the output writer
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | AKX |
Solution 2 | Anton Mett |
Solution 3 | Mykola Rudenko |
Solution 4 |