Reputation: 374
How could I save dfa as a pdf? So far I was able to have each of the elements saved separately in each file. How now can I have the whole file saved as a single pdf?
dfa = pd.DataFrame({'STREAM':['EAGLE','HAWK','HAWK','HAWK','EAGLE','HAWK','EAGLE'],'MAT':['A','D','F','D','C','C','E'],'KIS':['B','D','E','D','A','C','D'],'GEO':['B','C','E','E','F','A','B']})
dfa.to_csv('results.csv',index=False)
students_data = csv.reader(open("results.csv", 'r'))
for row in students_data:
STREAM = row[0]
MAT = row[1]
GEO = row[2]
KIS = row[3]
c = canvas.Canvas(MAT +".pdf")
c.drawString(60, 700, "STREAM: " + STREAM)
c.drawString(60, 600, "MAT: " + MAT)
c.drawString(60, 500, "KIS: " + KIS)
c.drawString(60, 400, "GEO: " + GEO)
c.save()
Upvotes: 1
Views: 593
Reputation: 56
My suggestion would be to create a blank PDF as your template page and then merge the new PDF to it
packet = io.BytesIO()
# Create the initial canvas.
c = canvas.Canvas(packet)
# your code for adding to the canvas
packet.seek(0)
new_pdf = PdfFileReader(packet)
# Import The Template
template = PdfFileReader(open('path_to_template'), "rb")
output = PdfFileWriter()
# add the created PDF as a watermark to the template
page = template.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# finally, write "output" to a real file
outputStream = open(output_path, "wb")
output.write(outputStream)
outputStream.close()
you will need these imports
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
Upvotes: 2