I was given some great code by 'tuomastik' on this site, and have tweaked it slightly to work for me. However, I have spent several hours trying to edit it so it prints to one PDF rather than multiple (one PDF, but each report starting on a fresh page), but my HTML isn't as good as I want to be, and I am getting stuck.
我在这个网站上给了'tuomastik'一些很棒的代码,并稍微调整了一下以便为我工作。但是,我花了几个小时试图编辑它,所以它打印到一个PDF而不是多个(一个PDF,但每个报告从一个新的页面开始),但我的HTML不如我想要的那样好,我我卡住了
The code is:
代码是:
HTML
HTML
<html>
<head>
<style type="text/css">
html, body {
width: 500px;
font-size: 12px;
background: #fff;
padding: 0px;
}
#my-custom-table {
width: 500px;
border: 0;
margin-top: 20px;
}
#my-custom-table td {
padding: 5px 0px 1px 5px;
text-align: left;
}
</style>
</head>
<body>
<table cellspacing="0" border="0" style="width:500px; border:0; font-size: 14px;">
<tr>
<td style="text-align:left;">
<b><span>Title of the PDF report - Row {{ row_ix + 1 }}</span></b>
</td>
<td style="text-align:right;">
<b><span>{{ date }}</span></b>
</td>
</tr>
</table>
<table cellspacing="0" border="0.1" id="my-custom-table">
{% for variable_name, variable_value in df.iteritems() %}
{% if loop.index0 == 0 %}
<tr style="border-top: 1px solid black;
border-bottom: 1px solid black;
font-weight: bold;">
<td>Variable name</td>
<td>Variable value</td>
</tr>
{% else %}
<tr>
<td>{{ variable_name }}</td>
<td>{{ variable_value }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
</body>
</html>
Python
蟒蛇
from datetime import date
import jinja2
import pandas as pd
from xhtml2pdf import pisa
df = pd.read_csv('data.csv', encoding='cp1252')
for row_ix, row in df.iterrows():
html = jinja2.Environment( # Pandas DataFrame to HTML
loader=jinja2.FileSystemLoader(searchpath='')).get_template(
'report_template.html').render(date=date.today().strftime('%d, %b %Y'),
row_ix=row_ix, df=row)
# Convert HTML to PDF
with open('report_row_%s.pdf' % (row_ix+1), "w+b") as out_pdf_file_handle:
pisa.CreatePDF(
src=html, # HTML to convert
dest=out_pdf_file_handle) # File handle to receive result
Could anyone help? I know I should remove the for loop from the python file, but not sure what to do with the html file.
有人可以帮忙吗?我知道我应该从python文件中删除for循环,但不知道如何处理html文件。
I hate asking, but I have given this a good attempt but I can't get it to run when I mess with the loops in html.
我讨厌问,但是我已经给了它一个很好的尝试但是当我弄乱html中的循环时我无法运行它。
Many thanks
非常感谢
1 个解决方案
#1
1
report_template.html
report_template.html
<html>
<head>
<style type="text/css">
html, body {
width: 500px;
font-size: 12px;
background: #fff;
padding: 0px;
}
#my-custom-table {
width: 500px;
border: 0;
margin-top: 20px;
}
#my-custom-table td {
padding: 5px 0px 1px 5px;
text-align: left;
}
</style>
</head>
<body>
{% for row_ix, row in df.iterrows() %}
<table cellspacing="0" border="0" style="width:500px; border:0; font-size: 14px;">
<tr>
<td style="text-align:left;">
<b><span>Title of the PDF report - Row {{ row_ix + 1 }}</span></b>
</td>
<td style="text-align:right;">
<b><span>{{ date }}</span></b>
</td>
</tr>
</table>
<table cellspacing="0" border="0.1" id="my-custom-table">
{% for variable_name, variable_value in row.iteritems() %}
{% if loop.index0 == 0 %}
<tr style="border-top: 1px solid black;
border-bottom: 1px solid black;
font-weight: bold;">
<td>Variable name</td>
<td>Variable value</td>
</tr>
{% else %}
<tr>
<td>{{ variable_name }}</td>
<td>{{ variable_value }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
<!-- Page break (the syntax is for xhtml2pdf) -->
<pdf:nextpage />
{% endfor %}
</body>
</html>
Python
蟒蛇
from datetime import date
import jinja2
import pandas as pd
from xhtml2pdf import pisa
df = pd.DataFrame({
"Average Introducer Score": [9, 9.1, 9.2],
"Reviewer Scores": ["Academic: 6, 6, 6", "Something", "Content"],
"Average Academic Score": [5.7, 5.8, 5.9],
"Average User Score": [1.2, 1.3, 1.4],
"Applied for (RC)": [9.2, 9.3, 9.4],
"Applied for (FEC)": [5.5, 5.6, 5.7],
"Duration (Months)": [36, 37, 38]})
html = jinja2.Environment( # Pandas DataFrame to HTML
loader=jinja2.FileSystemLoader(searchpath='')).get_template(
'report_template.html').render(date=date.today().strftime('%d, %b %Y'),
df=df)
# Convert HTML to PDF
with open('report.pdf', "w+b") as out_pdf_file_handle:
pisa.CreatePDF(
src=html, # HTML to convert
dest=out_pdf_file_handle) # File handle to receive result
#1
1
report_template.html
report_template.html
<html>
<head>
<style type="text/css">
html, body {
width: 500px;
font-size: 12px;
background: #fff;
padding: 0px;
}
#my-custom-table {
width: 500px;
border: 0;
margin-top: 20px;
}
#my-custom-table td {
padding: 5px 0px 1px 5px;
text-align: left;
}
</style>
</head>
<body>
{% for row_ix, row in df.iterrows() %}
<table cellspacing="0" border="0" style="width:500px; border:0; font-size: 14px;">
<tr>
<td style="text-align:left;">
<b><span>Title of the PDF report - Row {{ row_ix + 1 }}</span></b>
</td>
<td style="text-align:right;">
<b><span>{{ date }}</span></b>
</td>
</tr>
</table>
<table cellspacing="0" border="0.1" id="my-custom-table">
{% for variable_name, variable_value in row.iteritems() %}
{% if loop.index0 == 0 %}
<tr style="border-top: 1px solid black;
border-bottom: 1px solid black;
font-weight: bold;">
<td>Variable name</td>
<td>Variable value</td>
</tr>
{% else %}
<tr>
<td>{{ variable_name }}</td>
<td>{{ variable_value }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
<!-- Page break (the syntax is for xhtml2pdf) -->
<pdf:nextpage />
{% endfor %}
</body>
</html>
Python
蟒蛇
from datetime import date
import jinja2
import pandas as pd
from xhtml2pdf import pisa
df = pd.DataFrame({
"Average Introducer Score": [9, 9.1, 9.2],
"Reviewer Scores": ["Academic: 6, 6, 6", "Something", "Content"],
"Average Academic Score": [5.7, 5.8, 5.9],
"Average User Score": [1.2, 1.3, 1.4],
"Applied for (RC)": [9.2, 9.3, 9.4],
"Applied for (FEC)": [5.5, 5.6, 5.7],
"Duration (Months)": [36, 37, 38]})
html = jinja2.Environment( # Pandas DataFrame to HTML
loader=jinja2.FileSystemLoader(searchpath='')).get_template(
'report_template.html').render(date=date.today().strftime('%d, %b %Y'),
df=df)
# Convert HTML to PDF
with open('report.pdf', "w+b") as out_pdf_file_handle:
pisa.CreatePDF(
src=html, # HTML to convert
dest=out_pdf_file_handle) # File handle to receive result