Automate Multiple Excel Sheets and Produce Reports Using Python

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: 5330
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

This code will read and print data from multiple excel sheets, combine several excel sheets, do calculations and produce reports from multiple excel sheets. Run the code here. Requests is used to retrieve the excel sheet data from DropBox.

  1. import urllib.request
  2. import pandas as pd
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5.  
  6. urllib.request.urlretrieve("https://www.dropbox.com/scl/fi/54wsp05dmhvefj7mp5z1h/shift-data.xlsx?dl=1&rlkey=la2c42rft7mp33lh9u38eruv6", "shift-data.xlsx")
  7. urllib.request.urlretrieve("https://www.dropbox.com/scl/fi/7xmbxp3vluks1uux5cbj5/third-shift-data.xlsx?dl=1&rlkey=vgsk9ttpxi8txvd3rjjnekfyv", "third-shift-data.xlsx")
  8.  
  9. excel_file_1 = "shift-data.xlsx"
  10. excel_file_2 = "third-shift-data.xlsx"
  11.  
  12. #Read Excel sheets
  13. sheet1 = pd.read_excel(excel_file_1, sheet_name="first")
  14. sheet2 = pd.read_excel(excel_file_1, sheet_name="second")
  15. sheet3 = pd.read_excel(excel_file_2) #There is only one sheet
  16.  
  17. #Print data from excel sheet1
  18. #print(sheet1)
  19. #Print columns, e.g., with products
  20. #print(sheet1["Product"])
  21.  
  22. #Let's combine all data together
  23. all_sheets = pd.concat([sheet1, sheet2, sheet3])
  24. #Print new file
  25. print(all_sheets) #The number of columns is the same but rows increased, 87 rows X 6 columns
  26.  
  27. #Perform some calculations
  28. pivot = all_sheets.groupby(["Shift"]).mean()
  29. productivity = pivot.loc[:,"Production Run Time (Min)":"Products Produced (Units)"]
  30. print(productivity)
  31.  
  32. #Plot some graph
  33. charts = ["bar", "line", "barh", "hist", "box", "kde", "density", "area"]
  34. for chart_type in charts:
  35.     productivity.plot(kind="%s" % chart_type) #Replace bar with line, barh, hist, box, kde, density, area
  36.     plt.title("%s plot" % chart_type)
  37.     plt.show()
  38.  
  39. #Output data to new excel workbook
  40. all_sheets.to_excel("new_sheet.xlsx")


See attached graphical reports from this sample code.


Image

Image

Image

Image

Image


Data used:

Excel file 1, and excel file 2.
Attachments
Area_Plot.png
(21.09 KiB) Not downloaded yet
Area_Plot.png
(21.09 KiB) Not downloaded yet
Bar_Plot.png
(15.19 KiB) Not downloaded yet
Bar_Plot.png
(15.19 KiB) Not downloaded yet
Hbar_Plot.png
(15.08 KiB) Not downloaded yet
Hbar_Plot.png
(15.08 KiB) Not downloaded yet
Histogram.png
(17.29 KiB) Not downloaded yet
Histogram.png
(17.29 KiB) Not downloaded yet
Line_Plot.png
(23.15 KiB) Not downloaded yet
Line_Plot.png
(23.15 KiB) Not downloaded yet
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5330
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

Here is the related YouTube Video:

TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5330
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#3

Here is the excel sheet from Google Drive to play with.
Attachments
sales.xlsx
(118.77 KiB) Downloaded 46 times
sales.xlsx
(118.77 KiB) Downloaded 46 times
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