Add text to existing pdf using python

Adding text to an existing PDF using Python can be achieved in several ways. In this article, we will explore three different approaches to solve this problem.

Option 1: PyPDF2 Library

The PyPDF2 library is a popular choice for manipulating PDF files in Python. It provides a range of functionalities, including the ability to add text to an existing PDF.

import PyPDF2

def add_text_to_pdf(file_path, text):
    with open(file_path, 'rb') as file:
        pdf = PyPDF2.PdfFileReader(file)
        output = PyPDF2.PdfFileWriter()

        for page_num in range(pdf.getNumPages()):
            page = pdf.getPage(page_num)
            page.mergePage(page)
            page.addText(text)

            output.addPage(page)

        with open('output.pdf', 'wb') as output_file:
            output.write(output_file)

add_text_to_pdf('existing.pdf', 'Hello, World!')

This code snippet demonstrates how to use the PyPDF2 library to add text to an existing PDF. It opens the input PDF file, creates a new PDF writer object, and iterates over each page of the input PDF. For each page, it merges the page with itself, adds the desired text, and adds the modified page to the output PDF. Finally, it saves the modified PDF as ‘output.pdf’.

Option 2: pdfrw Library

The pdfrw library is another powerful tool for working with PDF files in Python. It allows you to read, write, and modify PDF documents.

import pdfrw

def add_text_to_pdf(file_path, text):
    template_pdf = pdfrw.PdfReader(file_path)
    output_pdf = pdfrw.PdfWriter()

    for page in template_pdf.pages:
        page.Contents.stream += f"nBT /F1 12 Tf 50 720 Td ({text}) Tj ET"

    output_pdf.addpages(template_pdf.pages)
    output_pdf.write('output.pdf')

add_text_to_pdf('existing.pdf', 'Hello, World!')

This code snippet demonstrates how to use the pdfrw library to add text to an existing PDF. It reads the input PDF file, creates a new PDF writer object, and iterates over each page of the input PDF. For each page, it appends a PDF content stream that adds the desired text at a specific position. Finally, it saves the modified PDF as ‘output.pdf’.

Option 3: PyMuPDF Library

The PyMuPDF library is a lightweight and efficient library for working with PDF files in Python. It provides a high-level interface for adding text to existing PDFs.

import fitz

def add_text_to_pdf(file_path, text):
    pdf = fitz.open(file_path)

    for page_num in range(pdf.page_count):
        page = pdf.load_page(page_num)
        page.insert_text((50, 50), text)

    pdf.save('output.pdf')

add_text_to_pdf('existing.pdf', 'Hello, World!')

This code snippet demonstrates how to use the PyMuPDF library to add text to an existing PDF. It opens the input PDF file, iterates over each page of the PDF, and inserts the desired text at a specific position. Finally, it saves the modified PDF as ‘output.pdf’.

Among the three options, the PyMuPDF library (Option 3) is the recommended choice for adding text to an existing PDF using Python. It provides a simple and efficient interface for manipulating PDF files and offers excellent performance. However, the choice ultimately depends on the specific requirements and preferences of the project.

Rate this post

7 Responses

  1. Option 3: PyMuPDF Library seems like the way to go! Sleek and efficient, just what I need for my PDF editing adventures. 💪

    1. I respectfully disagree. While PyPDF2 is a popular choice, I find the library lacking in terms of functionality and performance. There are better alternatives out there that offer more advanced features and better performance. Keep exploring, you might find something even better!

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents