Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Pandas DataFrame into PDF File format without pdfkit

I want to save a pandas dataframe into pdf format.

import pdfkit as pdf    
config = pdf.configuration(wkhtmltopdf="C:\Program Files\wkhtmltopdin\wkhtmltopdf.exe")
    pdf.from_url('http://google.com', 'out.pdf',configuration=config)
--> not working somehow even though I downloaded wkhtmltopdin on several different locations 

from weasyprint import HTML
HTML(string=pd.read_csv('cor.csv').to_html()).write_pdf("report.pdf")

dlopen() failed to load a library: cairo / cairo-2 / cairo-gobject-2
--> not working : Tried several times to solve this isseue, but cannot download library

I have tried 5 more packages and methods in stackoverflow and other websites but could not solve it.

Is there any more packages that I can try more? this giving me a cancer

Thanks in advance.

like image 381
Kevin Choi Avatar asked Dec 08 '22 14:12

Kevin Choi


2 Answers

One option is to start with:

df.to_html()

and then use QT to convert the HTML to PDF as follows:

from PyQt4.QtGui import QTextDocument, QPrinter, QApplication

import sys
app = QApplication(sys.argv)

doc = QTextDocument()
location = "c://apython//Jim//html//notes.html"
html = open(location).read()
doc.setHtml(html)

printer = QPrinter()
printer.setOutputFileName("foo.pdf")
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setPageSize(QPrinter.A4)
printer.setPageMargins(15, 15, 15, 15, QPrinter.Millimeter)

doc.print_(printer)
print("done!")

I obtained the second bit of code from html to pdf, and tested on Mac OSX with positive results.

like image 130
Philip DiSarro Avatar answered May 20 '23 13:05

Philip DiSarro


Have you considered drawing a Matplotlib Table, then exporting the Table Figure?

import matplotlib.backends.backend_pdf
import matplotlib.pyplot as plt
import pandas as pd

d = {'x{}'.format(i): range(30) for i in range(10)}

table = pd.DataFrame(d)

fig = plt.figure()

ax=fig.add_subplot(111)

cell_text = []
for row in range(len(table)):
    cell_text.append(table.iloc[row])

ax.table(cellText=cell_text, colLabels=table.columns, loc='center')
ax.axis('off')

pdf = matplotlib.backends.backend_pdf.PdfPages("output.pdf")
pdf.savefig(fig)
pdf.close()

I found this was simple, highly customizable and OS-independent (as far as I know). I was able to implement this on a client's server without downloading any additional packages.

like image 29
AJ Koenig Avatar answered May 20 '23 13:05

AJ Koenig