reportlab中的Pandas DataFrames [英] Pandas DataFrames in reportlab

查看:115
本文介绍了reportlab中的Pandas DataFrames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataFrame,并想将其输出为pdf.我目前正在尝试为此使用ReportLab,但它似乎无法正常工作.我在这里收到错误:

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:

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

make_pivot_table仅使用pandas pivot_table函数返回数据透视表. 我得到的错误是

make_pivot_table just returns a pivot table using pandas pivot_table function. The error I get is

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

我的问题是:

  1. 有什么方法可以使reportlab与DataFrames一起使用?
  2. 如果没有,我可以为同一目的使用什么包装?

推荐答案

Py

Hallo,

我还需要将某些Pandas DataFrame打印为.pdf来安排报告.我直接用df尝试了ReportLab,并遇到了"AttributeError:'DataFrame'对象没有属性'split'".我尝试使用df.values()并遇到"TypeError:'numpy.ndarray'对象不可调用"的情况.

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".

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

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:

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?

这似乎效率不高,我最初希望从ReportLab获得.看来我需要然后用某种方式来包装线..并且尺寸会改变...

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 ...

Fabio

=====

我现在对以下解决方案更加满意,而我对以上解决方案也不满意.

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

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

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 :-)

这里是:

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 ..

再见,法比奥.

这篇关于reportlab中的Pandas DataFrames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆