0.需求:根据excel另外一个sheet页面的几十万的表的某些列的数据,基于excel模板sheet页的字段对应,每行生成一个优美的pdf.
1.核心代码如下:
def exportPdf(retMap, i,txt,totalCount):
path_wkthmltopdf = 'wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
htmlCode = ''
with open('./template.htm', 'r', encoding='utf8', errors='ignore') as f:
htmlCode = f.read()
# 替换关键字
for k, v in retMap.items():
htmlCode = htmlCode.replace(k, str(v))
htmlCode = htmlCode.replace(k.replace('_','-'), str(v))
# print(k,str(v))
#替换币种
htmlCode=htmlCode.replace('# ', retMap['dill_flag'] + ' ')
#替换当前时间
htmlCode=htmlCode.replace('current_date',retMap['current_date'])
#命名
fname=f"{i+1} invoice({retMap['order_id']}).pdf"
pdfkit.from_string(htmlCode,fname , configuration=config)
import os
print('目录--------',os.getcwd())
txt.SetLabel('已完成{} %'.format((i+1)/totalCount*100))
print(' {} 完成'.format(fname))
2.excel模板及数据格式如下:
3.导出pdf效果及工具界面(支持批量生成):
微信交流 6686496