• Active Topics 

Automate Reports with Python and Pandas, Save the Output to HTML

Including Cython, Jython, IronPython, PyPy, Django framework, and interpreters: Ipython, IPython Jupyter/Notebook, CPython


Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5212
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

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
  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from datetime import datetime
  5.  
  6. #Download the data
  7. import urllib.request
  8. urllib.request.urlretrieve("https://www.dropbox.com/s/2dqqv1984h3mb5s/Placement_Data_Full_Class.csv?dl=1", "Placement_Data_Full_Class.csv")
  9.  
  10. data = pd.read_csv("Placement_Data_Full_Class.csv")
  11. #Show the first 5 top rows
  12. #print(data.head(5))
  13.  
  14. #Pivoting The Data:
  15. #Firstly, check the rows and columns you want to include in pivot table
  16. pivot=pd.pivot_table(
  17.                      data,
  18.                      values="sl_no",
  19.                      index=["degree_t","specialisation"],
  20.                      columns=["status","gender"], aggfunc=np.sum
  21.                      )
  22. pivot=pivot.fillna(0).astype(np.int64)
  23. #print(pivot)
  24.  
  25. #We can achieve the same by
  26. pivot=data.pivot_table(index=["degree_t","specialisation"],
  27.  columns=["status","gender"],
  28.  values="sl_no",aggfunc=np.sum).fillna(0)
  29. #print(pivot)
  30. #Let us make columns more readable
  31. new_columns = [
  32.                 'Unplaced_Females',
  33.                 'Placed_Females',
  34.                 'Unplaced_Males',
  35.                 'Placed_Males'
  36.                 ]
  37. pivot.columns = new_columns
  38. print(pivot)
  39.  
  40. #Let us plot the data
  41. plot = pivot.plot(kind="bar",figsize=(10.5,6))
  42. plot.tick_params(rotation=40)
  43. plot
  44. plt.title("Placement Bar Chart")
  45. plt.tight_layout()
  46. plt.show()
  47.  
  48. #Convert this report into HTML, itcan be styled by using CSS
  49. #Saving plot image to local file
  50. image = plot.get_figure().savefig('plot.png')
  51. image_tag = '<img src="plot.png" class="center">'
  52.  
  53. #writing HTML Content
  54. heading = '<h1> Automated Report </h1>'
  55. subheading = '<h3> Sample Report for Placement </h3>'
  56.  
  57. #Using .now() from datetime library to add Time stamp
  58. now = datetime.now()
  59. current_time = now.strftime("%m/%d/%Y %H:%M:%S")
  60. header = '<div class="top">' + heading + subheading +'</div>'
  61. footer = '<div class="bottom"> <h3 style="text-align: center;"> This Report has been Generated on '+ current_time +'</h3> </div>'
  62.  
  63. content = '<div class="table"> '+pivot.to_html()+' </div> <div class="chart"> '+ image_tag +'</div>'
  64.  
  65. #Concating everything to a single string
  66. html = header + content + footer
  67. # Adding CSS to HTML
  68. css = """<style> body {\n text-align:center; \n}\n table{\n margin:0px auto;\n}</style>"""
  69.  
  70. html = html + css
  71.  
  72. #Writing the file
  73. with open("report.html","w+") as file:
  74.  file.write(html)
  75.  
  76. #Produce pdf report
  77. #from weasyprint import HTML
  78. #HTML('report.html').write_pdf('report.pdf')

Attachments
report1.png
report.png
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Python Programming”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests