Reportlab 中的 Pandas DataFrames

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17542524/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:58:50  来源:igfitidea点击:

Pandas DataFrames in reportlab

pythonpython-2.7pandasreportlab

提问by Charles Dillon

I have a DataFrame, and want to output it to a pdf. I'm currently trying to use ReportLab for this, but it won't seem to work. I get an error here:

我有一个 DataFrame,想将它输出为 pdf。我目前正在尝试为此使用 ReportLab,但它似乎不起作用。我在这里收到一个错误:

        mytable = Table(make_pivot_table(data, pivot_cols, column_order, 'criterion'))

make_pivot_tablejust returns a pivot table using pandas pivot_tablefunction. The error I get is

make_pivot_table只使用 pandaspivot_table函数返回一个数据透视表。我得到的错误是

ValueError: <Table@0x13D7D0F8 unknown rows x unknown cols>... invalid data type

My questions are:

我的问题是:

  1. Is there any way to make reportlab work with DataFrames?
  2. If not, what package can I use for the same purpose?
  1. 有什么方法可以让 Reportlab 与 DataFrames 一起工作?
  2. 如果没有,我可以使用什么包来达到同样的目的?

回答by Fabio Pomi

Py

py

Hallo,

你好,

I'm also needing to print as .pdf some Pandas DataFrame to arrange reports. I tried ReportLab directly with df and had an "AttributeError: 'DataFrame' object has no attribute 'split'." I tried with df.values() and had "TypeError: 'numpy.ndarray' object is not callable".

我还需要将一些 Pandas DataFrame 打印为 .pdf 来安排报告。我直接用 df 尝试了 ReportLab 并且有一个“AttributeError:'DataFrame'对象没有属性'split'。” 我尝试了 df.values() 并且有“TypeError: 'numpy.ndarray' object is not callable”。

When close to quit the idea to build the .pdf report I tried str(df) and I had some outcome in the .pdf :-) ... The code looks like:

当接近退出构建 .pdf 报告的想法时,我尝试了 str(df) 并且我在 .pdf 中得到了一些结果:-) ...代码如下所示:

import pandas as pd
from reportlab.pdfgen import canvas
PATH_OUT = "C:\"
def pdf_df(c, testo, x, y):
    c.drawAlignedString(x,y, testo)
df = pd.DataFrame({'a':[3,4,5], 'b':[6,7,6],'c':[9,10,11]})
print df, type(df)
print''
df1 = (df['a'].groupby([df['b'], df['a']])).sum()
print df1, type(df1)
print ''
c = canvas.Canvas(PATH_OUT + 'out.pdf')
pdf_df (c, str(df), 300, 500)
pdf_df (c, str(df1), 300, 480)
c.showPage()
c.save()  

What do you think ? Might this make sense or there might be some 'smarter' way?

你怎么认为 ?这可能有意义还是可能有一些“更聪明”的方法?

This one seems not so effcient and I initially hoped to have from ReportLab. It seems I'd need then some way to wrap the lines .. and sizes will change ...

这个似乎不太有效,我最初希望从 ReportLab 那里得到。看来我需要一些方法来包装线条..并且尺寸会改变......

Fabio

法比奥

=====

======

I'm now much happier of the below solution, while I was not happy about the above one too.

我现在对下面的解决方案更满意,而我对上面的解决方案也不满意。

This is based on ReportLab grids, they work on lists. So the code is converting DF to list which is then treated as a ReportLab grid :-)

这是基于 ReportLab 网格,它们在列表上工作。所以代码将 DF 转换为列表,然后将其视为 ReportLab 网格 :-)

Here it is:

这里是:

from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import *
from reportlab.lib import colors
import pandas as pd
import random

PATH_OUT = "C:\"

elements = []
styles = getSampleStyleSheet()
doc = SimpleDocTemplate(PATH_OUT + 'Report_File.pdf')
elements.append(Paragraph("Report Title", styles['Title']))

data = [[random.random() for i in range(1,4)] for j in range (1,8)]
df = pd.DataFrame (data)
lista = [df.columns[:,].values.astype(str).tolist()] + df.values.tolist()

ts = [('ALIGN', (1,1), (-1,-1), 'CENTER'),
     ('LINEABOVE', (0,0), (-1,0), 1, colors.purple),
     ('LINEBELOW', (0,0), (-1,0), 1, colors.purple),
     ('FONT', (0,0), (-1,0), 'Times-Bold'),
     ('LINEABOVE', (0,-1), (-1,-1), 1, colors.purple),
     ('LINEBELOW', (0,-1), (-1,-1), 0.5, colors.purple, 1, None, None, 4,1),
     ('LINEBELOW', (0,-1), (-1,-1), 1, colors.red),
     ('FONT', (0,-1), (-1,-1), 'Times-Bold'),
     ('BACKGROUND',(1,1),(-2,-2),colors.green),
     ('TEXTCOLOR',(0,0),(1,-1),colors.red)]

table = Table(lista, style=ts)
elements.append(table)

doc.build(elements)

I'm really interested to read about other possible solutions ..

我真的很想了解其他可能的解决方案..

Bye, Fabio.

再见,法比奥。