如何使用jinja2和xhtml2pdf从熊猫数据框中生成多个pdf文件

时间:2018-10-28 13:21:50

标签: python html python-3.x pandas jinja2

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
{%for index, row in df.iterrows()%}
<h1><b>Credit Card Reward {{row['Customer Id']}}</b></h1>
<p>
 {% for Value_ix, value in df.iterrows() %}
    {{ row['message'] }}<p>
 {% endfor %}
 </p>
{% endfor %}
</body>
</html>

import pandas as pd
from xhtml2pdf import pisa
df = pd.DataFrame({"Id": [187763,127787,219846,197327,172830]})
df['Message'] = 'Dear\n\n ABC\n\n Its sad but you are not eligible for any 
offer.\n\n Regards\n XYZ'

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template("D:/Python_Files/Automation/report.html")
html_out = template.render(df=df)

with open('D:/Python_Files/Automation/report.pdf', "w+b") as 
out_pdf_file_handle:
pisa.CreatePDF(src=html_out,dest=out_pdf_file_handle)

对于每个ID,我想生成一个带有相应消息作为报告内容的pdf报告,我想通过每个ID的名称保存每个文件。

0 个答案:

没有答案