Pandas DataFrames in reportlab

2024/11/14 16:35:45

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

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?
Answer

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

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?

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.

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

Here it is:

from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import *
from reportlab.lib import colors
import pandas as pd
import randomPATH_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.

https://en.xdnf.cn/q/71989.html

Related Q&A

How to open and close a website using default browser with python

Im trying to write a python script on windows platform to open a webpage(such as Google), and then, after 10 seconds, close this website. Note: Im using Windows 7, Python 2.7.10, and IE

Comparing numpy array with itself by element efficiently

I am performing a large number of these calculations:A == A[np.newaxis].Twhere A is a dense numpy array which frequently has common values.For benchmarking purposes we can use:n = 30000 A = np.random.r…

Kivy: BoxLayout vs. GridLayout

BoxLayout(orientation=vertical) vs. GridLayout(cols=1):They both do the same thing, no? Is there a reason to choose one over the other?

Flask circular dependency

I am developing a Flask application. It is still relatively small. I had only one app.py file, but because I needed to do database migrations, I divided it into 3 using this guide:https://realpython.co…

How to create tox.ini variables

Is there a way to set arbitrary variables within tox.ini?An example would be a project name that might be used in a variety of ways. With a rather complex tox.ini, I find myself copy and pasting all …

How to apply json_normalize on entire pandas column

I have a dataframe with LISTS(with dicts) as column values . My intention is to normalize entire column(all rows). I found way to normalize a single row . However, Im unable to apply the same function …

Configure Vs code version 2.0.0 Build Task for python

I need help in configuring my Vs code to run scripts in python using Cntrl Shift B, I was working fine until Vs code upgraded to version 2.0.0 now it wants me to configure the Build. And I am clueless…

Generate N-Grams from strings with pandas

I have a DataFrame df like this: Pattern String 101 hi, how are you? 104 what are you doing? 108 Python is good to learn.I want to crea…

Merge dataframes on multiple columns with fuzzy match in Python

I have two example dataframes as follows:df1 = pd.DataFrame({Name: {0: John, 1: Bob, 2: Shiela}, Degree: {0: Masters, 1: Graduate, 2: Graduate}, Age: {0: 27, 1: 23, 2: 21}}) df2 = pd.DataFrame({Name: {…

Prevent Celery Beat from running the same task

I have a scheduled celery running tasks every 30 seconds. I have one that runs as task daily, and another one that runs weekly on a user specified time and day of the week. It checks for the "star…