是否可以使用 Python Pandas 构建报告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44001570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to build reports with Python Pandas?
提问by ScoutEU
I am currently using MS Access to produce reports but am somewhat limited with some of the calculations that I need to do.
我目前正在使用 MS Access 来生成报告,但我需要做的一些计算有些限制。
I was looking into perhaps using Python to run the reports i.e. one report per row of data which takes the column fields and puts them in text boxes going down, which looks like:
我正在考虑使用 Python 来运行报告,即每行数据一个报告,它采用列字段并将它们放入向下的文本框中,如下所示:
How would this be possible with Python?
这怎么可能用 Python 实现?
回答by tuomastik
This goes a little beyond Pandas, but you can easily create a PDF report from each row of your Pandas DataFrame with the help of jinja2
(template engine) and xhtml2pdf
(converts HTML into PDF) libraries.
这有点超出 Pandas,但您可以在jinja2
(模板引擎)和xhtml2pdf
(将 HTML 转换为 PDF)库的帮助下,从 Pandas DataFrame 的每一行轻松创建 PDF 报告。
First, define the structure and the looks of a report in 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>
<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" 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>
Then, run this Python 3 code, which converts each row of DataFrame into HTML string via jinja2
and then converts the HTML to PDF via xhtml2pdf
:
然后,运行此 Python 3 代码,它通过以下方式将 DataFrame 的每一行转换为 HTML 字符串jinja2
,然后通过xhtml2pdf
以下方式将 HTML 转换为 PDF :
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]})
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
For the DataFrame specified in the Python code, 3 PDFs will be outputted. The first PDF looks like this (converted to PNG to be able to show it here):
对于 Python 代码中指定的 DataFrame,将输出 3 个 PDF。第一个 PDF 看起来像这样(转换为 PNG 以便能够在此处显示):
回答by Xukrao
Reading in CSV files with Pandas: yes, definitely possible. See: http://pandas.pydata.org/pandas-docs/stable/io.html#io-read-csv-table
使用 Pandas 读取 CSV 文件:是的,绝对有可能。请参阅:http: //pandas.pydata.org/pandas-docs/stable/io.html#io-read-csv-table
Producing reports with Pandas: depends on what exactly you're looking for. Pandas has many different output writing functions but their focus is on producing tables, not on producing entire documents. The closest to a 'document'-style output that you can get directly from Pandas is probably the HTML table output: http://pandas.pydata.org/pandas-docs/stable/io.html#io-html
使用 Pandas 生成报告:取决于您究竟在寻找什么。Pandas 有许多不同的输出写入功能,但它们的重点是生成表格,而不是生成整个文档。您可以直接从 Pandas 获得的最接近“文档”样式输出的可能是 HTML 表格输出:http: //pandas.pydata.org/pandas-docs/stable/io.html#io-html
回答by P.Tillmann
That surely is possible but i don't think that pandas provides such a functionality. You might want to take a look at latex where you 'programm' and compile documents (which by itself has nothing to do with python). You can create a latex template and dynamically fill it with content in python and then compile a pdf document but it probably will take some effort to find your way into latex.
这当然是可能的,但我不认为 Pandas 提供这样的功能。您可能想看看在其中“编程”和编译文档的乳胶(这本身与 python 无关)。您可以创建一个 Latex 模板并用 python 中的内容动态填充它,然后编译一个 pdf 文档,但可能需要一些努力才能找到进入 Latex 的方法。