- 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')
-
- Active Topics
-
-
- by Eli 1 day ago Re: What is in Your Mind? View the latest post Replies 689 Views 275628
- by Eli 3 days ago Iran Launches Retaliatory Attack Against Israel, and Israel Retaliates by Attacking Iranian Isfahan Millitary Base View the latest post Replies 28 Views 921
- by Eli 4 days ago All in One: YouTube, TED, X, Facebook and Instagram Reels, Videos, Images and Text Posts View the latest post Replies 319 Views 10497
- by Eli 1 week ago Python Packages for Scientific Computing View the latest post Replies 8 Views 4390
- by Eli 1 week ago Dunia Yetu: Building Tanzania's Digital Future Together View the latest post Replies 5 Views 1870
- by Eli 1 week ago Russia Invades Ukraine View the latest post Replies 646 Views 211981
- by Eli 1 week ago Programmatically Move Files from One Folder to Another View the latest post Replies 6 Views 1438
- by Eli 2 weeks ago Collection of Greatest Christian Hymns of all Times View the latest post Replies 33 Views 45206
- by Eli 2 weeks ago What is Retrieval-Augmented Generation (RAG)? View the latest post Replies 2 Views 368
- by Eli 2 weeks ago Chat With ChatGPT - An Interactive Conversational AI View the latest post Replies 22 Views 25860
-
Automate Reports with Python and Pandas, Save the Output to HTML
- Eli
- Senior Expert Member
- Reactions: 183
- Posts: 5334
- Joined: 9 years ago
- Location: Tanzania
- Has thanked: 75 times
- Been thanked: 88 times
- Contact:
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
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
-
- Similar Topics
- Replies
- Views
- Last post
-
- Information
-
Who is online
Users browsing this forum: No registered users and 8 guests