Generating Automated & Customized Presentations Using Python

Smruthi S R
Brillio Data Science
4 min readApr 22, 2020

Creating business-consumable presentations and reports is a vital task in any Data science/Analytics project. Thinking about presentations/reports immediately reminds one of insightful charts, inferences from data analysis, statistical results and the impactful take-away that stakeholders can use.

Every time a fresh batch of data comes in, which required another version of the reports to be generated, wouldn’t it be a great idea to automate the process of report generation?

In a recent project, our stakeholders were researchers who wanted to have a comprehensive report to be generated, which contains the results of different types of descriptive and correlation analysis performed on continuous and categorical variables. This involved saving the charts and inferences in either PNG/JPEG and .txt format and used this information to generate PDF reports.

Here we will be discussing more about the code to generate automatic PDF reports and not get into the details of analysis performed on the dataset.

Below is an example report that is generated automatically using the FPDF package in python. Let’s explore how to use this package to generate reports in the following sections

Fig 1.1 - Sample automated PDF report

The PDF reports (Fig 1.1) contain two sections:

  1. Introduction page contains “Analysis Name” ,”Date” and “Company logo” (optional)
  2. Analysis & Insights pages contain different charts and inference information (this could vary depending on your dataset)

Some common steps in creating a report includes :-

  • Adding Header and Footer
  • Adding Name,Date and Logo(Optional)
  • Consistent Format of the PDF report
  • Designing the report (Alignment of charts, tables and text)
  • Saving the report

Let us now explore each of the above steps in detail. By the end of this post, one should be able to create their own PDF reports for any analysis and also automate the entire process.

Adding header,footer and page number :

To start with, create a class and define header & footer function. Specify a position of a header and footer using image object. The image object takes following arguments:

  1. Name : path / url of the image
  2. Abscissa : Refers to the distance along the horizontal axis(x-coordinate the point), here point refers to upper-left corner of the page
  3. Ordinate : Refers to the distance along the vertical axis (y-coordinate the point), here point refers to upper-left corner of the page
  4. W : Width of the image
  5. H: Height of the image
  6. Type : Image format (jpg,png etc)

The text in the header and footer can be edited using following commands:

  1. set_font : Used for setting font type and size
  2. set_text_color : Used for setting the font color
  3. cell object : Used to add page number
Adding header and footer to PDF

Construction of PDF page :

After defining the header and footer function, let us see how to construct a pdf. PDF object in FPDF package helps to define the format of the pdf.

It takes 3 arguments:

  1. Orientation : Page orientation possible values are Portrait (P) or Landscape(L)
  2. Unit : Format sizes are expressed using unit argument,possible values are point (pt) , millimeter (mm) ,centimeter (cm) and inch(in)
  3. Format : Format used for pdf pages, it can be A4,A3,A4,Letter etc

The following code constructs the PDF with object with Landscape orientation, A4 format measured in mm

PDF construction

Adding Name, Date and Logo (Optional) :

  • Use add_page() to add a new page. Alignment of the images, text can be done using pdf.w, pdf.lmargin, pdf.rmargin and pdf.get_y(), pdf.set_xy() commands as shown below:
First page of the report
After executing the above code first page of pdf report

Creating Analysis/Insights page in the report:

Public data set available in UCI (Pima Indians Diabetes Database) is used for the analysis here. Data set contains 767 observations and 9 variables. And for each variable in the data set Distribution chart, Statistical test values, Segment analysis and Inference information is generated.

The pdf page below,contains the all the aforementioned information for one of the variables called preg from the Pima dataset.

After executing below codes, second page of the report
  • Code to get the Distribution chart
  • Code to create the Table (Content of the table is added by importing a csv file)
Code to add statistical test information
  • Code to add segment (bar) chart
Code to add segment analysis information
  • Code to add the inference (Content of inference is added by importing a text file)
Code to add inference information

Use “for loop” to generate multiple pages in the pdf (one for each variable — each page can have more than one metric/visual/insight as shown above)

Final step is to save the PDF report :

Use pdf.output() command to save

Code to save PDF report

Conclusion

Generating reports through python as compared to visualization/reporting tools gives us complete control over the design/features of the report, reduces the effort to use external tools to publish reports, allows the user to share the report to multiple stake holders with ease. Also not to forget that python is open source language and it enables users to do statistical analysis, visualization & reporting all at one place.

Case Study

Automatic pdf generation was utilized as a tool tailored for one of our clients’ requirements. It facilitated the user to browse input data file, enter a parameters (choose the set of variables on which analyses have to be performed and criteria to filter some of the rows etc) & give a report name. The tool then performs back-end predefined statistical analysis and generates the PDF report automatically. The code was deployed in AWS production environment to make it more secure and efficient.

Complete python code is found here: https://github.com/Smruthi3/Generating-PDF-report-using-Python.git

For a detailed documentation of the syntax refer the below FPDF Manual https://pyfpdf.readthedocs.io/en/latest/Tutorial/index.html

I hope this post gives an idea on how to generate the production level pdf report using python’s FPDF package.

Feel free to post your questions/suggestions/comments below.

--

--