Sitemap

How to watermark your PDF files with Python.

3 min readAug 7, 2019

One of the most ancient yet efficient ways to protect your PDF work is by watermarking. In the past, we had to manually stamp it with a watermark when sending a PDF by e-mail to customers or relations.

Watermark PDF with Python and SchedulePython

In the following bullet points I will show you how we automated this in a simple, yet efficient manner!

First of all, we need to install Python. This can be done from the official website.

Not required, although highly recommended is the use of some sort of shared folder. Dropbox, OneDrive or Google Drive should work fine.

Let’s write some code!

The following dependencies should be installed:

pip install reportlab
pip install PyPDF2

The following code will waterstamp all pages from all files in the folder specified under folder_path.

Optionally you could choose to waterstamp with only a picture or only text, standard this script will do both. Make sure that your waterstamp image has the desired dimensions.

from reportlab.pdfgen import canvas
from PyPDF2 import PdfFileWriter, PdfFileReader
import os
"""
Refer to an image if you want to add an image to a watermark.
Fill in text if you want to watermark with text.
Alternatively, following settings will skip this.
picture_path = None
text = None
"""
picture_path = 'company_logo.png'
text = 'Company Name'
# Folder in which PDF files will be watermarked. (Could be shared folder)
folder_path = './mydir'
c = canvas.Canvas('watermark.pdf')
if picture_path:
c.drawImage(picture_path, 15, 15)
if text:
c.setFontSize(22)
c.setFont('Helvetica-Bold', 36)
c.drawString(15, 15,text)
c.save()
watermark = PdfFileReader(open("watermark.pdf", "rb"))
for file in os.listdir(folder_path):
if file.endswith(".pdf"):
output_file = PdfFileWriter()
input_file = PdfFileReader(open(folder_path + '/'+ file, "rb"))
page_count = input_file.getNumPages()for page_number in range(page_count):
input_page = input_file.getPage(page_number)
input_page.mergePage(watermark.getPage(0))
output_file.addPage(input_page)
output_path = folder_path + '/'+ file.split('.pdf')[0] + '_watermarked' + '.pdf'
with open(output_path, "wb") as outputStream:
output_file.write(outputStream)

Let’s run it, everyday!

Once you have tested and tweaked the code to your liking you will probably want to run it repeatedly on a PC.

One of the easiest ways to schedule (and monitor this) is to use the PyPi package ‘SchedulePython’. This tool allows you to run Python scripts on multiple desktop environments and lets you know what the output was. This gives you control about which tasks ran successful and which didn’t (and why).

Step 1: Register on the SchedulePython website

SchedulePython.com signup

Step 2: Run the following commands in your command line:

pip install schedulepython
schedulepython

Step 3: Login with your SchedulePython credentials:

SchedulePython Login

Step 4: Restart your computer

Step 5: After logging in to SchedulePython you can add your watermarking script by copy-pasting it in the editor.

Now you can simply choose when to schedule it or start it remotely by pressing the ‘play button’ and keep track of the outcome on SchedulePython.com.

There you go, no more manual watermarking. Robotic stamping for the win!

--

--

Samantha O.
Samantha O.

Written by Samantha O.

Computer scientist. Fears spiders and Artificial general intelligence. ❤ Travelling : 6/7 contintents checked.

Responses (1)