How to convert Pandas dataframe to PDF file in Python

In this python tutorial, I will show you the most efficient way to generate PDF for dataframes in Pandas.

Are you in a Hurry?

We will generate a PDF documents out of a dataframe in the following ways.

Method No 1: Using the Matplotlib Plots to generate PDF from a dataframe

Method No 2: Using sqlite3 module in Python to generate a Pdf file from a dataframe

Method No 3: Using the wsprint library In python to generate PDF from a pandas dataframe

Method No 1. Using the Matplotlib library in Python to generate PDF from a dataframe

Using the matplotib library in python, we can first convert the pandas dataframe into a table and then use the pdfPages() function to generate the pdf file from that table.

Use the following procedure to convert a pandas dataframe to a pdf file in python

i). install the following libraries one by one using pip

Install Pandas to have a dataframe


pip install pandas

Install Numpy to convert dataframe to numpy array


pip install numpy

Install matplotlib to gernarate a table in python


pip install matplotlib

ii). import the following modules


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

iii). Convert the pandas dataframe to a table using matplotlib


df = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3"))
fig, ax =plt.subplots(figsize=(12,4))
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=df.values,colLabels=df.columns,loc='center')

iv). Save the Pdf File that have the pandas dataframe


pp = PdfPages("foo.pdf")
pp.savefig(fig, bbox_inches='tight')
pp.close()

The complete python code that convert a pandas dataframe to a pdf file in python


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

df = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3"))


fig, ax =plt.subplots(figsize=(12,4))
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=df.values,colLabels=df.columns,loc='center')


pp = PdfPages("foo.pdf")
pp.savefig(fig, bbox_inches='tight')
pp.close()

Output of the code

Method No 2: Converting a pandas dataframe to a pdf in python

In this method, we have used three python libraries to convert a pandas dataframe into converting a pandas dataframe to a pdf file.

The following is the best method to convert a pandas dataframe to a pdf file.

  1.  Create a dataframe
  2.  convet the dataframe to html file
  3.  Convert the HTML file to pdf
  4.  save the pdf file

Following is the code that converts a pandas dataframe into a pdf file using the above procedure.


import pandas as pd
import pdfkit as pdf
import sqlite3
import numpy as np

df = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3"))
df.to_html('f.html')
nazivFajla='z.pdf'
pdf.from_file('f.html', nazivFajla)

Method No 3: Using the wsprint Library to generate pdf file from a pandas dataframe in python

In this method, we will use some CSS to prettify our generated pdf file in python. We will just Jupiter notebook to get the results.

follow the following procedure to gernate the pdf file from the pandas dataframe in python

1. First save the following CSS code in the current Jupiter notebook directory.


  .mystyle {
    font-size: 11pt; 
    font-family: Arial;
    border-collapse: collapse; 
    border: 1px solid silver;

}

.mystyle td, th {
    padding: 5px;
}

.mystyle tr:nth-child(even) {
    background: #E0E0E0;
}

.mystyle tr:hover {
    background: silver;
    cursor: pointer;
  }

2. Now run the following python code in the notebook cell.


pdf_filepath = os.path.join(folder,file_pdf)
demo_df = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3"))

table=demo_df.to_html(classes='mystyle')

html_string = f'''
<html>
  <head><title>HTML Pandas Dataframe with CSS</title></head>
  <link rel="stylesheet" type="text/css" href="df_style.css"/>
  <body>
    {table}
  </body>
</html>
'''

HTML(string=html_string).write_pdf(pdf_filepath, stylesheets=["df_style.css"])

Summary and conclusion

We have learned how we can convert a pandas dataframe to a pdf file in python. If you have any questions please let me know in the comment section.

Scroll to Top