mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-27 14:01:12 +00:00
Create pywatermark.py
This commit is contained in:
parent
d080931b09
commit
fcc36f9360
42
scripts/PDF Watermark/pywatermark.py
Normal file
42
scripts/PDF Watermark/pywatermark.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
from PyPDF4 import PdfFileWriter, PdfFileReader
|
||||
import PyPDF4
|
||||
|
||||
|
||||
PyPDF4.PdfFileReader('input.pdf')
|
||||
|
||||
|
||||
def put_watermark(input_pdf, output_pdf, watermark):
|
||||
|
||||
# reads the watermark pdf file through
|
||||
# PdfFileReader
|
||||
watermark_instance = PdfFileReader(watermark)
|
||||
|
||||
# fetches the respective page of
|
||||
# watermark(1st page)
|
||||
watermark_page = watermark_instance.getPage(0)
|
||||
|
||||
# reads the input pdf file
|
||||
pdf_reader = PdfFileReader(input_pdf)
|
||||
|
||||
# It creates a pdf writer object for the
|
||||
# output file
|
||||
pdf_writer = PdfFileWriter()
|
||||
|
||||
# iterates through the original pdf to
|
||||
# merge watermarks
|
||||
for page in range(pdf_reader.getNumPages()):
|
||||
|
||||
page = pdf_reader.getPage(page)
|
||||
|
||||
# will overlay the watermark_page on top
|
||||
# of the current page.
|
||||
page.mergePage(watermark_page)
|
||||
|
||||
# add that newly merged page to the
|
||||
# pdf_writer object.
|
||||
pdf_writer.addPage(page)
|
||||
|
||||
with open(output_pdf, 'wb') as out:
|
||||
|
||||
# writes to the respective output_pdf provided
|
||||
pdf_writer.write(out)
|
Loading…
Reference in New Issue
Block a user