Home >Backend Development >Python Tutorial >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:
Python 2.7 Example:
To add text to an existing PDF using Python 2.7, follow these steps:
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>
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>
Create a new PDF writer:
<code class="python">output = PdfFileWriter()</code>
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>
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:
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>
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>
Create a new writer:
<code class="python">output = PdfFileWriter()</code>
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>
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!