Automate Reports with Python and Pandas, Save the Output to HTML
Posted: Fri Mar 05, 2021 2:22 am
This code (adopted from here) automates some report for the mock placement data taken from Kaggle. The generated tabular report and chart are saved to HTML. Run this code at programming-and-computing-with-python-r ... forum-6300
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- from datetime import datetime
- #Download the data
- import urllib.request
- urllib.request.urlretrieve("https://www.dropbox.com/s/2dqqv1984h3mb5s/Placement_Data_Full_Class.csv?dl=1", "Placement_Data_Full_Class.csv")
- data = pd.read_csv("Placement_Data_Full_Class.csv")
- #Show the first 5 top rows
- #print(data.head(5))
- #Pivoting The Data:
- #Firstly, check the rows and columns you want to include in pivot table
- pivot=pd.pivot_table(
- data,
- values="sl_no",
- index=["degree_t","specialisation"],
- columns=["status","gender"], aggfunc=np.sum
- )
- pivot=pivot.fillna(0).astype(np.int64)
- #print(pivot)
- #We can achieve the same by
- pivot=data.pivot_table(index=["degree_t","specialisation"],
- columns=["status","gender"],
- values="sl_no",aggfunc=np.sum).fillna(0)
- #print(pivot)
- #Let us make columns more readable
- new_columns = [
- 'Unplaced_Females',
- 'Placed_Females',
- 'Unplaced_Males',
- 'Placed_Males'
- ]
- pivot.columns = new_columns
- print(pivot)
- #Let us plot the data
- plot = pivot.plot(kind="bar",figsize=(10.5,6))
- plot.tick_params(rotation=40)
- plot
- plt.title("Placement Bar Chart")
- plt.tight_layout()
- plt.show()
- #Convert this report into HTML, itcan be styled by using CSS
- #Saving plot image to local file
- image = plot.get_figure().savefig('plot.png')
- image_tag = '<img src="plot.png" class="center">'
- #writing HTML Content
- heading = '<h1> Automated Report </h1>'
- subheading = '<h3> Sample Report for Placement </h3>'
- #Using .now() from datetime library to add Time stamp
- now = datetime.now()
- current_time = now.strftime("%m/%d/%Y %H:%M:%S")
- header = '<div class="top">' + heading + subheading +'</div>'
- footer = '<div class="bottom"> <h3 style="text-align: center;"> This Report has been Generated on '+ current_time +'</h3> </div>'
- content = '<div class="table"> '+pivot.to_html()+' </div> <div class="chart"> '+ image_tag +'</div>'
- #Concating everything to a single string
- html = header + content + footer
- # Adding CSS to HTML
- css = """<style> body {\n text-align:center; \n}\n table{\n margin:0px auto;\n}</style>"""
- html = html + css
- #Writing the file
- with open("report.html","w+") as file:
- file.write(html)
- #Produce pdf report
- #from weasyprint import HTML
- #HTML('report.html').write_pdf('report.pdf')