使用python的fpdf批量生成pdf文件

1、首先cmd下载fpdf包
pip install fpdf

2、调用fpdf包,先实现写入数据的功能

from fpdf import FPDF
pdf=FPDF()
pdf.add_page()#新建一页
pdf.set_xy(0,0)
pdf.set_font('arial','B',14)#设置字体
pdf.cell(60)#将定位向右移动60个单位
pdf.cell(70,10,'The transcript',0,2,'C')
pdf.cell(-40)#将定位向左移动40个单位
pdf.cell(50,10,'number',1,0,'C')
pdf.cell(40,10,'name',1,0,'C')
pdf.cell(40,10,'score',1,2,'C')
pdf.cell(-90)#将定位向下移动90个单位
pdf.set_font('arial','',12)
pdf.cell(50,10,'10010',border=1,ln=0,align='C')
pdf.cell(40,10,'lilei',border=1,ln=0,align='C')
pdf.cell(40,10,'97',border=1,ln=0,align='C')
pdf.ln()#换行
pdf.cell(10)#将定位向右移动10个单位
pdf.cell(50,10,'10012',border=1,ln=0,align='C')
pdf.cell(40,10,'wangbin',border=1,ln=0,align='C')
pdf.cell(40,10,'69',border=1,ln=0,align='C')

pdf.output('C:\\Users\\caoming\\Desktop\\20200914数据处理\\Excel2PDF.pdf', 'F')

生成如下内容
在这里插入图片描述
3、如果想单独生成每个人的成绩单,就可以配合pandas批量写入,将excel中的内容按照姓名分表,再按照一定格式生成个人成绩单。

import pandas as pd
import os
import selectUsecols as su
from fpdf import FPDF
lujing=r'C:\Users\caoming\Desktop\新建文件夹'
file_name='chengjidan.xls'
data_name=os.path.join(lujing,file_name)
wb=pd.read_excel(data_name)
names=su.hee(data_name,[1])
print(names)
pdf = FPDF()
pdf.add_page()  # 新建一页
pdf.set_xy(0, 0)
pdf.set_font('arial', 'B', 14)  # 设置字体
pdf.cell(60)  # 将定位向右移动60个单位
pdf.cell(70, 10, 'The transcript', 0, 2, 'C')
pdf.cell(-40)  # 将定位向左移动40个单位
pdf.cell(50, 10, 'number', 1, 0, 'C')
pdf.cell(40, 10, 'name', 1, 0, 'C')
pdf.cell(40, 10, 'subject', 1, 0, 'C')
pdf.cell(40, 10, 'score', 1, 2, 'C')
pdf.cell(-130)  # 将定位向左移动90个单位
for name in names:
    chenji=wb[wb['name']==name]
    chenji_list = chenji.values.tolist()
    n=len(chenji_list)
    print(n)
    for i in range(n):
        pdf.set_font('arial', '', 12)
        pdf.cell(50, 10, str(chenji_list[i][0]), border=1, ln=0, align='C')
        pdf.cell(40, 10, chenji_list[i][1], border=1, ln=0, align='C')
        pdf.cell(40, 10, chenji_list[i][2], border=1, ln=0, align='C')
        pdf.cell(40, 10, str(chenji_list[i][3]), border=1, ln=0, align='C')
        pdf.ln()  # 换行
        pdf.ln()  # 换行

        pdf.output(lujing+'\\'+name+'.pdf', 'F')
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值