The PyPDF2 as of date of writing has depreciated the PdfFileReader, PdfFileWriter and few other methods and changed it to different names and methods and has also changed methods like getPage() directly to attribute of PdfReader.
Here is a very Simple Class to add text to existing pdf file:
(Use is demonstrated at end)
from PyPDF2 import PdfWriter, PdfReader, Transformation
import io
from reportlab.pdfgen.canvas import Canvas
class GenerateFromTemplate:
def __init__(self,template):
self.template_pdf = PdfReader(open(template, "rb"))
self.template_page= self.template_pdf.pages[0]
self.packet = io.BytesIO()
self.c = Canvas(self.packet,pagesize=(self.template_page.mediabox.width,self.template_page.mediabox.height))
def addText(self,text,point):
self.c.drawString(point[0],point[1],text)
def merge(self):
self.c.save()
self.packet.seek(0)
result_pdf = PdfReader(self.packet)
result = result_pdf.pages[0]
self.output = PdfWriter()
op = Transformation().rotate(0).translate(tx=0, ty=0)
result.add_transformation(op)
self.template_page.merge_page(result)
self.output.add_page(self.template_page)
def generate(self,dest):
outputStream = open(dest,"wb")
self.output.write(outputStream)
outputStream.close()
"""
Use as:
gen = GenerateFromTemplate("template.pdf")
gen.addText("Hello!",(100,200))
gen.addText("PDF!",(100,300))
gen.merge()
gen.generate("Output.pdf")
"""
Hope this helps.