Python 如何保存PDF

Python 如何保存PDF,python,pandas,Python,Pandas,如何将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保存为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()

我的建议是创建一个空白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()
你需要这些进口货

from PyPDF2 import PdfFileWriter, PdfFileReader
import io

你看到这篇帖子了吗?@Balaji,这并不是我真正关心的事情。我看到了。请删除template=PdfFileReader(open('path_to_template'),“rb”))中的尾部)我导入什么才能使其工作?这个我已经进口了。从reportlab.pdfgen导入canvas@Ptar我在底部的答案中包含了导入。此代码返回以下错误PyPDF2.utils.PdfReadError:无法读取空文件