Skip to content

Instantly share code, notes, and snippets.

@gil-obradors
Forked from kzim44/editExistingPdf.py
Created April 17, 2019 18:59
Show Gist options
  • Save gil-obradors/69d5505d1f63aca3b9ae6ebcefacb9e8 to your computer and use it in GitHub Desktop.
Save gil-obradors/69d5505d1f63aca3b9ae6ebcefacb9e8 to your computer and use it in GitHub Desktop.
Edit an existing PDF using Python
from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
packet = StringIO.StringIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
can.drawString(100,100, "Hello world")
can.save()
#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(file("mypdf.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("/home/joe/newpdf.pdf", "wb")
output.write(outputStream)
outputStream.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment