如何保存PDF

如何将dfa保存为pdf? 到目前为止,我已经能够将每个元素分别保存在每个文件中。现在如何将整个文件另存为单个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()
评论
  • 3、2、1
    3、2、1 回复

    我的建议是创建一个空白PDF作为模板页面,然后 合并新的PDF到它

    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()