Convert dataframe into pdf report in Python

Introduction

To create PDF in python there are many approaches. pdfkit is one of the better approaches. It renders HTML into PDF with various image formats, HTML forms, and other complex printable documents.

Install pdfkit

Install package "pdfkit" in python using anaconda prompt.

In [ ]:
pip install pdfkit

To make pdfkit work perfectly install wkhtmltopdf which deals with images and other complex things.To download wkhtmltopdf follow the link: http://wkhtmltopdf.org/.

Sample data

Import the data and generate a pivot table as well as some summary statistics of the average quantity and price of the CPU and Software sales.

Convert "basic_salary_1_P2.csv" data into excel (.xlsx) format.

Import pandas and pdfkit

In [1]:
# import pandas to create dataframe
import pandas as pd
# import pdfkit to convert the file into pdf
import pdfkit
# read the excel file as a dataframe
df = pd.read_excel('basic_salary_1_P2.xlsx', index_col=0)
df.head()
Out[1]:
Last_Name Grade Location ba ms
First_Name
Alan Brown GR1 DELHI 17990 16070.0
Agatha Williams GR2 MUMBAI 12390 6630.0
Rajesh Kolte GR1 MUMBAI 19250 14960.0
Ameet Mishra GR2 DELHI 14780 9300.0
Neha Rao GR1 MUMBAI 19235 15200.0

Converting above data into html

Create a html file first in the folder where your code is present.

Convert the dataframe into html code and write it to the html file created earlier.

In [2]:
f = open('exp.html','w')
a = df.to_html()
f.write(a)
f.close()

As wkhtmltopdf software was installed earlier. Add the path to the folder in the environment. Copy this wkhtmltopdf folder to the folder where your code is present. Then convert the html file into a pdf file using:

In [3]:
pdfkit.from_file('exp.html', 'example.pdf')
Loading pages (1/6)
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)
Done                                                                      
Out[3]:
True