1

I am looking to combine the matplotlib and reportlab libraries to create a pdf report. I specifically want to display a dataframe in this pdf. After some research, I managed to get this code:

import numpy as np
import pandas as pd


from reportlab.platypus import SimpleDocTemplate
from reportlab.lib.pagesizes import letter


from reportlab.platypus import Table


df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo'], 'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C': np.random.randn(8),'D': np.random.randn(8)})
data = df.groupby('A').sum()

print(data)
filename = 'dataframe.pdf'

pdf = SimpleDocTemplate(filename, pagesize = letter)


lista = [data.columns[:,].values.astype(str).tolist()] + data.values.tolist()
table = Table(lista)

#PDF construction
elems = []
elems.append(table)

pdf.build(elems)

I get this result on a pdf : https://zupimages.net/viewer.php?id=20/10/t22g.png

I find the rendering not terrible, especially since it misses the title of the axes on the left. I understand that it should be placed in the lista variable, but I don't really see the syntax.

Another solution would be to export the dataframe to html, and convert it to png. for that, i installed the pypandoc library, but impossible to find on the internet a code which would convert html to png...

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712

1 Answers1

1

I was struggling with the same question - how to combine Matplotlib and Reportlab.

The way it can be done is the following: you will separately create a plot, save this down as a temporary picture, then automatically fetch this picture and add it to the pdf. I was sceptical against this initial but have used it for a longer while now it is a very good solution, it is quick and accurate.

I recommend you to study this SO answer, you need also to install pdfrw.

Is there a matplotlib flowable for ReportLab?

Jaco
  • 1,564
  • 2
  • 9
  • 33