Home  >  Article  >  Backend Development  >  How to Add Text to Existing PDFs Using Python?

How to Add Text to Existing PDFs Using Python?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-22 14:56:02690browse

How to Add Text to Existing PDFs Using Python?

Add Text to Existing PDF Using Python

Background:

Often, you may need to add additional text to an existing PDF document. Fortunately, Python offers several modules that simplify this task. However, it's important to identify modules compatible with both Windows and Linux systems.

Recommended Module:

After considering various options, two suitable modules are PyPDF2 and PyPDF. These modules provide a high level of functionality and cross-platform support.

Code Examples:

Below are code examples for both Python 2.7 and Python 3.x:

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>

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.addPage(page)
# Finally, write "output" to a real file
output_stream = open("destination.pdf", "wb")
output.write(output_stream)
output_stream.close()</code>

These code examples will add the text "Hello world" to the first page of the existing PDF file and save the result to a new PDF file. You can customize the text and position accordingly.

The above is the detailed content of How to Add Text to Existing PDFs Using Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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