Home  >  Article  >  Backend Development  >  How to Add Text to Existing PDFs in Python: A Step-by-Step Guide

How to Add Text to Existing PDFs in Python: A Step-by-Step Guide

Patricia Arquette
Patricia ArquetteOriginal
2024-10-22 14:26:03152browse

How to Add Text to Existing PDFs in Python: A Step-by-Step Guide

Adding Text to Existing PDFs with Python: A Comprehensive Solution

Introduction:
Adding text to existing PDFs in Python can be a valuable task for various applications. This article provides a detailed guide on how to accomplish this using a combination of Python modules, including pyPdf and ReportLab.

Python Libraries for PDF Manipulation:
To get started, install the following modules:

  • pyPdf: For reading and writing PDFs
  • ReportLab: For adding text and graphics to PDFs

Python 2.7 Example:
To add text to an existing PDF using Python 2.7, follow these steps:

  1. Import the necessary modules:

    <code class="python">from pyPdf import PdfFileWriter, PdfFileReader
    import StringIO
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter</code>
  2. Create a new PDF with the text you want to add using ReportLab:

    <code class="python">packet = StringIO.StringIO()
    can = canvas.Canvas(packet, pagesize=letter)
    can.drawString(10, 100, "Hello world")
    can.save()</code>
  3. Create a new PDF writer:

    <code class="python">output = PdfFileWriter()</code>
  4. Merge the new page with the existing PDF:

    <code class="python">existing_pdf = PdfFileReader(file("original.pdf", "rb"))
    page = existing_pdf.getPage(0)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)</code>
  5. Save the modified PDF:

    <code class="python">outputStream = file("destination.pdf", "wb")
    output.write(outputStream)
    outputStream.close()</code>

Python 3.x Example:
For Python 3.x, the code is slightly different:

  1. Import the modules:

    <code class="python">from PyPDF2 import PdfFileWriter, PdfFileReader
    import io
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter</code>
  2. Create a new PDF with text using ReportLab:

    <code class="python">packet = io.BytesIO()
    can = canvas.Canvas(packet, pagesize=letter)
    can.drawString(10, 100, "Hello world")
    can.save()</code>
  3. Create a new writer:

    <code class="python">output = PdfFileWriter()</code>
  4. Merge the pages:

    <code class="python">existing_pdf = PdfFileReader(open("original.pdf", "rb"))
    page = existing_pdf.pages[0]
    page.merge_page(new_pdf.pages[0])
    output.add_page(page)</code>
  5. Save the file:

    <code class="python">output_stream = open("destination.pdf", "wb")
    output.write(output_stream)
    output_stream.close()</code>

By using these examples, you can effectively add text or other elements to existing PDFs using Python and the appropriate libraries. This provides a powerful tool for modifying and enhancing PDF documents for various use cases.

The above is the detailed content of How to Add Text to Existing PDFs in Python: A Step-by-Step Guide. 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