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