Better Data Science - Generate PDF Reports With Python
Better Data Science - Generate PDF Reports With Python
Python
● Install any library you don't have with pip install <libraryname> command
● It's likely you won't have FPDF installed, so install it with:
○ pip install fpdf
In [1]:
import os
import shutil
import numpy as np
import pandas as pd
import calendar
from datetime import datetime
from fpdf import FPDF
Data generation
● construct() function makes a directory for plots and than makes a sales chart for
every month in 2020 except January
○ January was excluded because we want to show how you can
have different number of elements on reports page
○ Feel free to include it
■ Change for i in range(2, 13) to for i in range(1, 13)
● Once the visualizations are saved, they are appended to a list of list structure
(matrix)
○ Max of 3 elements per row
○ Can be lower
○ A single row in this matrix represents a single page
■ If the row has 3 elements, the report page will
have 3 visualizations
In [5]:
PLOT_DIR = 'plots'
def construct():
# Delete folder if exists and create it again
try:
shutil.rmtree(PLOT_DIR)
os.mkdir(PLOT_DIR)
except FileNotFoundError:
os.mkdir(PLOT_DIR)
temp.append(f'{PLOT_DIR}/{fname}')
counter += 1
PDF class
def header(self):
# Custom logo and positioning
# Create an `assets` folder and put any wide and short image inside
# Name the image `logo.png`
self.image('assets/logo.png', 10, 8, 33)
self.set_font('Arial', 'B', 11)
self.cell(self.WIDTH - 80)
self.cell(60, 1, 'Sales report', 0, 0, 'R')
self.ln(20)
def footer(self):
# Page numbers in the footer
self.set_y(-15)
self.set_font('Arial', 'I', 8)
self.set_text_color(128)
self.cell(0, 10, 'Page ' + str(self.page_no()), 0, 0, 'C')
pdf.output('SalesRepot.pdf', 'F')