How to generate Reports with Python (3 Formats/4 Tools)
 As Excel, HTML, PDF

Lianne & Justin

Lianne & Justin

python report generation
Source: Unsplash

In this tutorial, we’ll show you how to generate reports with Python.

Reporting is one of the essential tasks for anyone who works with data information. It is critical but also tedious. To free our hands and minds, we can make a program to automate the report generation process. Besides data analysis, Python is also convenient for automating routine tasks such as reporting.

Following this guide, you’ll use tools with Python to generate reports, as the below common formats:

  • Excel
  • HTML (with template)
  • HTML to PDF
  • PDF directly

If you are looking to generate reports automatically with Python, this tutorial is a great starting point. You’ll know where to start generating your next report!

We are going to show you popular and easy-to-use Python tools, with examples. The example report will include data tables and a chart, the two most common elements within reports.

Let’s get started!


To follow this tutorial, you’ll need to know:



Overview of Python reporting

Before we start, let’s look at an overview of reporting with Python.

The standard formats of reports are Excel, HTML, and PDF. The good news is, Python can generate reports in all these formats. So you can choose any of these formats, depending on the needs of the report’s users.

Below is a summary of what we’ll cover in this tutorial. We’ll need pandas for all the reports, since we need to manipulate and analyze data when building reports.

To generate the report asTools/LibrariesUse case examples
Excelpandasif you want to use the report for further analysis in Excel
HTMLpandasif you want to embed the report in the email body, or on a website
HTML with templatepandas & Jinja2Same as above
but for more complicated, repetitive reports, it’s better to use a template
HTML to PDFpandas & WeasyPrintif you need both HTML and PDF formats
if you are good with HTML, but need PDF as the final format
PDF directlypandas & FPDFif you only want PDF documents

Create the example report

To show examples of the above, we’ll use stock market data. We’ll generate simple reports based on the historical data of the S&P 500 index from Yahoo! finance.

Below is the code to pull the data for the reports. In summary, we grab the historic data of the S&P 500 and make a summary statistics table based on it.

At the end, you should have two pandas DataFrames sp500_history and sp500_history_summary for reporting:

  • sp500_history: the 10 years historical data of S&P 500
    Your result will look different than below, since the date changes according to the date when you run the code.
DateOpenHighLowCloseVolumeClose_200ma
2011-07-011320.6400151341.0100101318.1800541339.6700443796930000NaN
2011-07-051339.5899661340.8900151334.3000491337.8800053722320000NaN
2011-07-061337.5600591340.9399411330.9200441339.2199713564190000NaN
2011-07-071339.6199951356.4799801339.6199951353.2199714069530000NaN
2011-07-081352.3900151352.3900151333.7099611343.8000493594360000NaN
2021-06-224224.6098634255.8398444217.2700204246.43994132087600003806.545059
2021-06-234249.2700204256.6000984241.4301764241.83984431724400003810.619458
2021-06-244256.9702154271.2797854256.9702154266.49023431416800003815.292709
2021-06-254274.4501954286.1201174271.1601564280.70019562483900003819.701410
2021-06-284284.8999024288.4101564276.0400394276.3999022265967013824.387460

2514 rows × 6 columns

  • sp500_history_summary: a simple summary statistics of the above data
OpenHighLowCloseVolumeClose_200ma
count2514.0000002514.0000002514.0000002514.0000002.514000e+032315.000000
mean2303.2939132314.9091132290.7755982303.7913293.784200e+092276.846098
std719.274017722.527344715.816729719.4140979.192366e+08609.384256
min1097.4200441125.1199951074.7700201099.2299802.265967e+081272.574702
25%1806.2399601810.2350161800.6274721805.2699893.246950e+091860.605498
50%2122.1700442128.6999512112.8300782123.8399663.602310e+092096.223644
75%2795.9174802807.9000242779.9424442797.3775634.099572e+092763.536024
max4284.8999024288.4101564276.0400394280.7001959.878040e+093824.387460

Besides these two DataFrames, we’ll also create a line chart showing the series of Close and Close_200ma.

The code below builds the line chart using the matplotlib and seaborn libraries, and saves it as a PNG file on your local computer. This file chart.png will be used in the reports below.

python report line chart
chart.png

Now we have everything needed (sp500_history, sp500_history_summary, chart.png) to generate reports.

Further learning: Please note we generated the above simple report quickly for demonstration only.
To learn about using Python for data analysis, please check out our course Python for Data Analysis with projects.
The course covers the pandas, seaborn libraries. You’ll learn how to manipulate data, create data visualizations, etc., which is essential to create reports in Python.

Let’s dive into each specific example!

Excel

We’ll start with the classic: Excel. Excel is a widely used, powerful data analysis and visualization tool. In many scenarios, you may want to store the report within Excel and share it with others. Others can easily open the spreadsheet, examine the report, and even use it for further analysis in Excel, Python, or other programs.

While there are different tools to save reports as Excel, we’ll use the foundation one: ExcelWriter and the to_excel method in pandas. Here is the summary of the steps. You can read it together with the code below.

  1. Set up an ExcelWriter with engine ‘openpyxl’
    You can also use the engine ‘xlsxwriter’. The general procedure is the same, but the syntax will be different. We are using ‘openpyxl’ since it’s the default engine for xlsx files.
  2. Export the data from Python to Excel
    We export DataFrames sp500_history and sp500_history_summary to two separate sheets/tabs.
  3. Add a line chart in Excel showing the data of Close and Close_200ma
    Please check the comments in the code below for details.
    Since Excel is a data visualization tool, we are adding charts directly in Excel. But this is essentially the same chart as the chart.png file we’ve created earlier.

The final saved Excel file has two tabs that look like below:

There is much more styling you can accomplish with this method. Please check out openpyxl documentation.

HTML

HTML (Hyper Text Markup Language) is the standard markup language for creating web pages. We can embed an HTML format report easily on a web page, or an email. So it is also popular for different use cases.

We’ll cover two main methods of generating HTML reports in Python. One is the basic one, and the other is to generate one with templates using the library called Jinja2.

Let’s start with the basic one. We can define HTML code as a Python string, and write/save it as an HTML file.

Here are the general steps of the procedure. You can read it together with the code below.

  1. Set up multiple variables to store the titles, text within the report
  2. Combine them as a long f-string of code
    Within the f-string, we define the HTML document, including:
    • the head: contains meta information about the HTML page, including the title
    • the body: a container for all the visible contents, such as h1, p, img, table
      Within the img element, we include the chart.png file saved on your computer.
      To include the DataFrame as HTML tables, we use the to_html method. For simplicity, we only render the last 3 rows of the DataFrame sp500_history.
  3. Write the html string as an HTML file

You can open the final HTML report in any modern browser. You can try to match each of the HTML elements to the final report below.

python generate html report

We’ll leave the report as it is. If you want to style this HTML report more, please learn about CSS (Cascading Style Sheets). With CSS, you can control almost everything, including the color of text, font, spacing between elements, background color, and so on.

HTML with template

Besides the basic approach to generate HTML reports, we can also use a templating engine like Jinja2. With Jinja2, we can put special placeholders in the template to allow writing code similar to Python syntax; then the template is passed data to render the final document.

So we can easily create an HTML template, then use it with Python-like syntax. This is especially useful when you are using complicated templates for reporting over and over again.

Below are the general steps to use Jinja2 to generate HTML reports:

  1. Create an HTML template with variables
    I named it ‘report_template.html’, and saved it under a folder in the current working directory called ‘templates’.
python set up jinja html template file

Within such a file, I put in the code below.
We are creating the same HTML report as the previous example, but the syntax of Jinja‘s template is different from Python strings. You can read more about its syntax here.
For example, two curly brackets {{...}} are used as placeholders for expressions like variables. We can specify such variable’s value later within this template, which you’ll see soon.

  1. Create a template Environment object, which will be used to load templates
    loader=FileSystemLoader('templates') tells Jinja to look for templates stored within a file/folder called ‘templates’. You can use other loaders to load templates in other ways or from other locations, please check the doc for details.
  2. Load the template from the Environment
    We load the template file set up earlier called ‘report_template.html’.
  3. Render the template with variables
    As you can see, the variables within {{...}} in ‘report_template.html’ are set as specific values. Jinja will embed them within the template.
  4. Write the template to an HTML file
    You can open the HTML file in any modern web browser. It should look the same as the previous example of HTML report.
python generate html report with jinja

You might wonder, Jinja2 doesn’t seem to be very impressive in this example? It seems similar to the basic approach, except that we use some Jinja2 methods versus the f-string. But imagine if you have a much more complicated report, and you want to reuse it, then Jinja would make it much easier.

HTML to PDF

Now you have the report in HTML format, what if you also want PDF documents to share with a broader audience? We can use a package called WeasyPrint to convert HTML to PDF.

Using the below code, we convert the HTML file ‘html_report_jinja.html’ to a PDF file called ‘weasyprint_pdf_report.pdf’, with an inline CSS stylesheet. Within the stylesheet, we specified the page size, margin, and the table header and cell border.

The final result is a nice looking PDF below.

python convert html to pdf reports

PDF directly

What if you’re not familiar with HTML, you just want the PDF format report? Python also has a solution for that. We’ll use this Python package called FPDF.

I would strongly suggest you read the short minimal example tutorial on the FPDF website before using the package.

The challenge to generate our report with FPDF is to show the tables of data. So before using FPDF, we define a function below to loop through the columns and rows of a DataFrame to display it on cells in the PDF.

Then, we can use FPDF to display the report. The general procedure is:

  1. Set up basic setting for a PDF page with FPDF
  2. Lay out each item we want to see in the report
    Please see the details in the comments below. It is helpful to match the code with the final PDF.
  3. Output the PDF file

The final PDF report looks like below.

python create pdf reports

You might have heard about another popular tool called ReportLab. We picked FPDF since it seems to be easier to learn.


In summary, you’ve learned examples of using Python to generate reports as Excel, HTML (with template), and PDF.

Each of them is good for different scenarios:

  • If you want an Excel file, then do Excel
  • If you want to embed HTML to web pages, or are just good at HTML, use HTML. You can also convert HTML to PDF after
  • If you only want PDF, you can go with PDF directly too

Each of these methods/packages has a lot more techniques. Hope you got the basics within this tutorial, and are ready to explore details on your own!

After generating reports using Python, it is also convenient to automatically send emails to share the reports. Please check out How to Send Emails using Python: Tutorial with examples.

Further learning: if you want an interactive web-based report/dashboard, we highly recommend plotly Dash.
You can either take our comprehensive introductory course: Python Interactive Dashboards with Plotly Dash, or read our article with an example: 6 Steps to Interactive Python Dashboards with Plotly Dash.

We’d love to hear from you. Leave a comment for any questions you may have or anything else.

Twitter
LinkedIn
Facebook
Email
Lianne & Justin

Lianne & Justin

7 thoughts on “How to generate Reports with Python (3 Formats/4 Tools)<br /><div style='color:#7A7A7A;font-size: large;font-family:roboto;font-weight:400;'> As Excel, HTML, PDF</div>”

  1. Hi
    Thanks.

    Is there a way to create nice tables with nice headers and colour formatting and add dynamic data from dataframes into them and then paste those tables in Word? I meed to produce a final document in Word. I’m using the docx library now.

    Is there a way to include a table of contents in your Word document and your html document from python?

Leave a Comment

Your email address will not be published. Required fields are marked *

More recent articles

Scroll to Top

Learn Python for Data Analysis

with a practical online course

lectures + projects

based on real-world datasets

We use cookies to ensure you get the best experience on our website.  Learn more.