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