DEV Community

Cover image for Edit PDF files with Python
Stokry
Stokry

Posted on

Edit PDF files with Python

I like to test Python in different situations, it just makes my life easier in many situations. Python allows me to automate things that are normally repeated and boring, so I can focus on important aspects of my job. Today I will show you how can you edit PDF files with python.

In this example I will be using ReportLab.
The ReportLab Toolkit. An Open Source Python library for generating PDFs and graphics.

This is our original pdf:
enter image description here

Let's jump to the code!

First we need to import dependencies

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
Enter fullscreen mode Exit fullscreen mode

First we will create a new PDF with Reportlab, in this part we will also define our font color and the font size:

packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=letter)
can.setFillColorRGB(1, 0, 0)
can.setFont("Times-Roman", 14)
can.drawString(72, 655, "Hello from Python")
can.save()
Enter fullscreen mode Exit fullscreen mode

Then we move to the beginning of the StringIO buffer:

packet.seek(0)
new_pdf = PdfFileReader(packet)
Enter fullscreen mode Exit fullscreen mode

read your existing PDF

existing_pdf = PdfFileReader(open("original.pdf", "rb"))
output = PdfFileWriter()
Enter fullscreen mode Exit fullscreen mode

The we add the "watermark" (which is the new pdf) on the existing page;

page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
Enter fullscreen mode Exit fullscreen mode

And finally, write "output" to a real file:

outputStream = open("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()
Enter fullscreen mode Exit fullscreen mode

This is our result:

enter image description here

Thank you all.

Discussion (5)

Collapse
tombutlersings profile image
tombutlersings • Edited on

Just adding on here, if you'd like to add all of your pages from your old PDF to your new PDF, then use the following in conjunction with the code above:

pdf_pages = existing_pdf.numPages
i = 0
while i < pdf_pages:
page = existing_pdf.getPage(i)
output.addPage(page)
i += 1

outputStream = open("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()

Collapse
expoashish profile image
Ashish Yadav

Traceback (most recent call last):
File "main.py", line 5, in
can = canvas.Canvas(packet, pagesize=letter)
NameError: name 'packet' is not defined

what is packet here?????????????????

Collapse
stokry profile image
Stokry Author

Ohh thank you, I made one typo, I updated my code: this part was missing:

packet = io.BytesIO()
Enter fullscreen mode Exit fullscreen mode