Home > Backend Development > Python Tutorial > How to add text to existing PDF using Python and External Modules?

How to add text to existing PDF using Python and External Modules?

DDD
Release: 2024-10-22 15:30:03
Original
379 people have browsed it

How to add text to existing PDF using Python and External Modules?

Add Text to Existing PDF using Python

When working with PDF documents, there are often occasions where additional text needs to be added. This could range from simple annotations to complex watermarks. As there is no built-in Python library for editing PDFs, external modules must be employed to achieve this functionality.

PyPDF and ReportLab

PyPDF and ReportLab are two popular options for manipulating PDFs in Python. However, neither of these modules provides direct support for editing existing PDF files. They are primarily used for creating new PDFs with custom content.

Combining PyPDF and ReportLab

To add text to an existing PDF, a combination of PyPDF and ReportLab can be used. Here's a detailed example that works on both Windows and Linux:

Example Code:

Python 2.7:

<code class="python">from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter

packet = StringIO.StringIO()
can = canvas.Canvas(packet, pagesize=letter)
can.drawString(10, 100, "Hello world")
can.save()

# move to the beginning of the StringIO buffer
packet.seek(0)

# create a new PDF with Reportlab
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(file("original.pdf", "rb"))
output = PdfFileWriter()
# 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)
# finally, write "output" to a real file
outputStream = file("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()</code>
Copy after login

Python 3.x:

<code class="python">from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter

packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=letter)
can.drawString(10, 100, "Hello world")
can.save()

# move to the beginning of the StringIO buffer
packet.seek(0)

# create a new PDF with Reportlab
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(open("original.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.pages[0]
page.merge_page(new_pdf.pages[0])
output.add_page(page)
# finally, write "output" to a real file
output_stream = open("destination.pdf", "wb")
output.write(output_stream)
output_stream.close()</code>
Copy after login

This solution effectively combines the flexibility of ReportLab for creating watermark text with the page manipulation capabilities of PyPDF.

The above is the detailed content of How to add text to existing PDF using Python and External Modules?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template