Watermarking PDFs with ChatGPT
I wanted to write python code that watermarks every page of a PDF file to save myself some time. Did it work?
TL;DR: Yes! I am now the proud owner of WatermarkPdf2.py code that allows me to watermark any PDF with whatever text I want.
I copied all the code into VS code and saved it down as a python file. Then I installed the PyPDF2 and reportlab packages, and ran the code… and got an error.
I copy pasted the error into ChatGPT:
I then copy paste in my code from VS Code and ask it to adjust it according to PdfReader instead of PdfFileReader and PdfFileWriter.
ChatGPT adjusted the code as needed, and explained what changes it made. Then I copied this new code into VS code.
I ran the python code again…. and got another error. So I copy-pasted the error into ChatGPT again. Also to note, when I copy paste in an error, I usually provide no explanation and ChatGPT will know what I need:
Most of these errors are likely because packages have been updated since September 2021 (ChatGPT’s data cutoff). This also happened with mergePage and addPage (now they are merge_page and add_page). After a few more iterations:
It worked! No… it didn’t. When I opened up the “watermarked.pdf” file. There was no watermark. And there was nothing in the “watermark.pdf” file either. It was empty.
So like a Karen asking for the manager, I complained about the lack of watermark:
Great, then I asked it to include these changes inside my code if they already don’t exist. And I ran the code again.
And… nothing. I also decided to increase the opacity and the size of the text:
I copy pasted this new code into a new file called “WatermarkPdf2.py” just in case this didn’t work either and I didn’t to go back to the older version of the file. Then I adjusted the mediabox, addPage, mergePage accordingly. I ran the code and:
It worked!
Pretty amazing! This basically took me <30 minutes to put together.