Advertisement
Orctopusaurus

Python Watermarker

Apr 25th, 2024
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | Source Code | 0 0
  1. import os
  2. import PyPDF2
  3. from reportlab.pdfgen import canvas
  4. from reportlab.lib.utils import ImageReader
  5. import io
  6.  
  7. # Step 1 - define what it means to add a watermark to an input-doc and to save it to an output-doc
  8. # This does not require any changes, except where indicated below
  9.  
  10. def add_watermark(input_pdf, output_pdf, watermark_png):
  11.     with open(input_pdf, 'rb') as pdf_file:
  12.         pdf_reader = PyPDF2.PdfReader(pdf_file)
  13.         pdf_writer = PyPDF2.PdfWriter()
  14.  
  15.         watermark_pdf = canvas.Canvas(io.BytesIO())
  16.  
  17.         watermark_pdf.setFillAlpha(0.5)
  18.         # the number we input into setFillAlpha determines the transparency of the image
  19.         # 0.0 - fully transparent
  20.         # 1.0 - fully opaque
  21.         # my default setting is
  22.         # watermark_pdf.setFillAlpha(0.5)
  23.  
  24.         watermark_pdf.drawImage(ImageReader(watermark_png), 100, 200, width=400, height=400)
  25.         # the numbers here determine the position and size of the watermark, you can play around with these
  26.         # my default setting is
  27.         # watermark_pdf.drawImage(ImageReader(watermark_png), 100, 200, width=400, height=400)
  28.  
  29.         watermark_pdf.save()
  30.  
  31.         watermark_page = PyPDF2.PdfReader(io.BytesIO(watermark_pdf.getpdfdata())).pages[0]
  32.  
  33.         for page_num in range(len(pdf_reader.pages)):
  34.             page = pdf_reader.pages[page_num]
  35.             page.merge_page(watermark_page)
  36.             pdf_writer.add_page(page)
  37.  
  38.         with open(output_pdf, 'wb') as output_file:
  39.             pdf_writer.write(output_file)
  40.  
  41. # Step 2 - We must define the paths where the program is supposed to work
  42. # You NEED to edit the value for these 3 variables
  43.  
  44. input_folder = r'C:\Users\Anwender\Downloads\Gavel\Input'
  45. # you NEED to edit this
  46. # the value be of the format r'[the address of your folder for documents you want watermarked]'
  47. # Example:
  48. # [the address of your folder for documents you want watermarked] = C:\Users\Anwender\Downloads\Gavel\Input
  49. # then
  50. # input_folder = r'C:\Users\Anwender\Downloads\Gavel\Input'
  51.  
  52. output_folder = r'C:\Users\Anwender\Downloads\Gavel\Output'
  53. # you NEED to edit this
  54. # the value should be of the format r'[the address of your folder for the documents once the watermark has been added]'
  55. # Example:
  56. # [the address of your folder for the documents once the watermark has been added] = C:\Users\Anwender\Downloads\Gavel\Output
  57. # then
  58. # output_folder = r'C:\Users\Anwender\Downloads\Gavel\Output'
  59.  
  60. watermark_png_path = r'C:\Users\Anwender\Downloads\Gavel\Watermark_Gavel.png'
  61. # you NEED to edit this
  62. # the value should be of the format r'[the address of your file that contains the watermark image]'
  63. # Example:
  64. # [the address of your file that contains the watermark image] = C:\Users\Anwender\Downloads\Gavel\Watermark_Gavel.png
  65. # then
  66. # watermark_png_path = r'C:\Users\Anwender\Downloads\Gavel\Watermark_Gavel.png'
  67.  
  68. # Step 3 - Loop through all PDF files in the input folder
  69. # You don't need to do anything here
  70.  
  71. for filename in os.listdir(input_folder):
  72.   if filename.endswith(".pdf"):
  73.     # Extract filename without extension
  74.     file_name, _ = os.path.splitext(filename)
  75.  
  76.     # Construct input and output paths
  77.     input_pdf_path = os.path.join(input_folder, filename)
  78.     output_pdf_path = os.path.join(output_folder, f"{file_name}_WM.pdf")
  79.  
  80.     # Add watermark to the current file
  81.     add_watermark(input_pdf_path, output_pdf_path, watermark_png_path)
  82.     print(f"Watermarked: {filename} -> {file_name}_WM.pdf")
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement