Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 21, 2021 11:13 am GMT

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, PdfFileReaderimport iofrom reportlab.pdfgen import canvasfrom reportlab.lib.pagesizes import letter

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

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()

Then we move to the beginning of the StringIO buffer:

packet.seek(0)new_pdf = PdfFileReader(packet)

Then we need to read our existing PDF:

existing_pdf = PdfFileReader(open("original.pdf", "rb"))output = PdfFileWriter()

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)

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

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

This is our result:

enter image description here

Thank you all.


Original Link: https://dev.to/stokry/edit-pdf-files-with-python-1e1j

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To