Python sales
See attached!
2 years ago
12
Biggestsales.odt
17-ReadingExcel.pdf
SalesData1.xlsx
Biggestsales.odt
One example program that we covered in class found the largest sales that we made during the current sales period. That program found the amount of the sale, but it did not tell us who made that big sale or on what date. So, alter the program so that it prints the name of the salesperson who made the biggest sale, the amount of that sale, and the date of the sale.
The spreadsheet of sales data is attached. Just submit your revision of my program.
17-ReadingExcel.pdf
Reading Data from Excel Spreadsheets
Program Zero - Just read a sheet's title, its dimensions, and cell A1
# demo program for reading Excel data
from openpyxl import load_workbook
workbook = load_workbook ("demofile.xlsx", read_only=True)
sheet = workbook.active
print ("Sheet Title =", sheet.title) # title of this sheet
print ("Cell A1 =", sheet["A1"]) # not really want you want
print ("Cell A1's value =", sheet["A1"].value) # you want the value
# which cells are used?
print ("The sheet uses cells",sheet.calculate_dimension())
Program One - Use a for loop to read every cell of a spreadsheet
# for loop to read all cells
# each cell has three parts: column,row,value
from openpyxl import load_workbook
workbook = load_workbook ("demofile.xlsx", read_only=True)
sheet = workbook.active
for row in sheet.rows: # for each row used by this sheet
for cell in row: # get each cell
print("Row", cell.row, end=" ")
print("Column",cell.column, end=" = ")
print(cell.value)
Program Two - Read just values
Notice when the program above is run it says cell C3 contains a formula. If you want just the
values then change the load command to:
workbook = load_workbook ("demofile.xlsx",
read_only=True,
data_only=True)
Instead of reading every row, we can pick which rows and columns that we want. We can either
read row-by-row or column-by-column.
Given the spreadsheet:
Why Hello There
12 34 56
Program Three creates:
---- Next Row ----
Why
Hello
There
---- Next Row ----
12
34
56
Program Four creates:
---- Next Column ----
Why
12
---- Next Column ----
Hello
34
---- Next Column ----
There
56
Program Three # demo of iter_rows
from openpyxl import load_workbook
# open the SalesData spreadsheet
workbook = load_workbook ("SalesData.xlsx")
sheet = workbook.active
# grab rows 1-3 and all 7 columns
cellData = sheet.iter_rows(min_row=1, max_row=2,
min_col=1, max_col=3)
# iterate through each row
for nextRow in cellData :
print ("---- Next Row ----")
for cell in nextRow :
print(cell.value)
Program Four # demo of iter_cols
from openpyxl import load_workbook
workbook = load_workbook ("SalesData.xlsx")
sheet = workbook.active
# grab rows 1-3 and all 7 columns
cellData = sheet.iter_cols(min_row=1, max_row=2,
min_col=1, max_col=3)
# iterate through each row
for nextCol in cellData :
print ("---- Next Column ----")
for cell in nextCol :
print(cell.value)
Notice that iter_rows and iter_cols gives us back a bunch of data. It is a bit tricky to put the cell
values out of that data. In the examples above, two nested for loops were used to pull out the cell
values. There are other ways to take values out of a big grid of data.
The iter_rows functions gives us back a list of lists. (Actually, it's a tuple generator.) Given the
spreadsheet above, iter_rows builds some data where the first item looks like this:
(Why, Hello, There)
We can put those values in a variable named nextRow, then grab each piece with their index.
For example, nextRow[0], nextRow[1], nextRow[2]. So, nextRow[0] would have Why the first
time through the loop and 12 the second time through the loop.
Program Five -- demo15.py
Print cells A1:C4 of our SalesData. The four rows show up on four separate lines. The print
adds a few dashes between the values so the columns stand apart more clearly.
# grab rows 1-4 and columns 1-3
from openpyxl import load_workbook
# open the SalesData spreadsheet
workbook = load_workbook ("SalesData.xlsx")
sheet = workbook.active
gridData = sheet.iter_rows(min_row=1, max_row=4,
min_col=1, max_col=3,
values_only=True)
# gridData is not a list, so this print doesn't show much
print (gridData)
print ("-=-=-=-=-=-")
# show the three items for each row
for nextRow in gridData :
print (nextRow[0],nextRow[1],nextRow[2], sep=" --- ")
Program Six
Now let's grab just one column and print all the values in that column.
Notice that max_row was not given, so iter_rows will give us every row that has data.
# grab rows from column 7 (total sales)
from openpyxl import load_workbook
# open the SalesData spreadsheet
workbook = load_workbook ("SalesData.xlsx")
sheet = workbook.active
# grab column G, except title cell G1
gridData = sheet.iter_rows ( min_row=2,
min_col=7, max_col=7,
values_only=True )
# print the value of each cell in that data
for nextRow in gridData :
print ( nextRow[0] )
And finally, instead of printing everything in row 7, let's put the values into a list. We can do
lots of things with a list. In this example, we use the built-in max function to find our biggest
particular sale.
Program Seven - demo17.py
# Make a *list* of Total Sales
from openpyxl import load_workbook
# open the SalesData spreadsheet
workbook = load_workbook ("SalesData.xlsx")
sheet = workbook.active
# grab column G, except title cell G1
gridData = sheet.iter_rows ( min_row=2,
min_col=7, max_col=7,
values_only=True)
# convert that data into a list
salesList = []
for nextRow in gridData :
salesList.append(nextRow[0])
# print the max value in the list
print("Largest Sale was", max(salesList) )
So, now we know the amount of our biggest sale. But, we don't know when that sale was made
or who made it. That data was in columns we did not retrieve. Matching that big sale to a
salesperson is our next class.
SalesData1.xlsx
Sheet1
| Date | Region | Rep | Item | Units | Cost | Total |
| 1/6/21 | East | Jones | Pencil | 95 | 1.99 | 189.05 |
| 1/23/21 | Central | Kivell | Binder | 50 | 19.99 | 999.50 |
| 2/9/21 | Central | Jardine | Pencil | 36 | 4.99 | 179.64 |
| 2/26/21 | Central | Gill | Pen | 27 | 19.99 | 539.73 |
| 3/15/21 | West | Sorvino | Pencil | 56 | 2.99 | 167.44 |
| 4/1/21 | East | Jones | Binder | 60 | 4.99 | 299.40 |
| 4/18/21 | Central | Andrews | Pencil | 75 | 1.99 | 149.25 |
| 5/5/21 | Central | Jardine | Pencil | 90 | 4.99 | 449.10 |
| 5/22/21 | West | Thompson | Pencil | 32 | 1.99 | 63.68 |
| 6/8/21 | East | Jones | Binder | 60 | 8.99 | 539.40 |
| 6/25/21 | Central | Morgan | Pencil | 90 | 4.99 | 449.10 |
| 7/12/21 | East | Howard | Binder | 29 | 1.99 | 57.71 |
| 7/29/21 | East | Parent | Binder | 81 | 19.99 | 1,619.19 |
| 8/15/21 | East | Jones | Pencil | 35 | 4.99 | 174.65 |
| 9/1/21 | Central | Smith | Desk | 2 | 125.00 | 250.00 |
| 9/18/21 | East | Jones | Pen Set | 16 | 15.99 | 255.84 |
| 10/5/21 | Central | Morgan | Binder | 28 | 8.99 | 251.72 |
| 10/22/21 | East | Jones | Pen | 64 | 8.99 | 575.36 |
| 11/8/21 | East | Parent | Pen | 15 | 19.99 | 299.85 |
| 11/25/21 | Central | Kivell | Pen Set | 96 | 4.99 | 479.04 |
| 12/12/21 | Central | Smith | Pencil | 67 | 1.29 | 86.43 |
| 12/29/21 | East | Parent | Pen Set | 74 | 15.99 | 1,183.26 |
| 1/15/22 | Central | Gill | Binder | 46 | 8.99 | 413.54 |
| 2/1/22 | Central | Smith | Binder | 87 | 15.00 | 1,305.00 |
| 2/18/22 | East | Jones | Binder | 4 | 4.99 | 19.96 |
| 3/8/22 | West | Sorvino | Binder | 7 | 19.99 | 139.93 |
| 3/25/22 | Central | Jardine | Pen Set | 50 | 4.99 | 249.50 |
| 4/11/22 | Central | Andrews | Pencil | 66 | 1.99 | 131.34 |
| 4/28/22 | East | Howard | Pen | 96 | 4.99 | 479.04 |
| 5/15/22 | Central | Gill | Pencil | 53 | 1.29 | 68.37 |
| 6/1/22 | Central | Gill | Binder | 80 | 8.99 | 719.20 |
| 6/18/22 | Central | Kivell | Desk | 5 | 125.00 | 625.00 |
| 7/5/22 | East | Jones | Pen Set | 62 | 4.99 | 309.38 |
| 7/22/22 | Central | Morgan | Pen Set | 55 | 12.49 | 686.95 |
| 8/8/22 | Central | Kivell | Pen Set | 42 | 23.95 | 1,005.90 |
| 8/25/22 | West | Sorvino | Desk | 3 | 275.00 | 825.00 |
| 9/11/22 | Central | Gill | Pencil | 7 | 1.29 | 9.03 |
| 9/28/22 | West | Sorvino | Pen | 76 | 1.99 | 151.24 |
| 10/15/22 | West | Thompson | Binder | 57 | 19.99 | 1,139.43 |
| 11/1/22 | Central | Andrews | Pencil | 14 | 1.29 | 18.06 |
| 11/18/22 | Central | Jardine | Binder | 11 | 4.99 | 54.89 |
| 12/5/22 | Central | Jardine | Binder | 94 | 19.99 | 1,879.06 |
| 12/22/22 | Central | Andrews | Binder | 28 | 4.99 | 139.72 |
- For Kim Woods
- When do I get my reflection paper back
- theatre assignment
- need this today! last assignment
- PSY 300 Week 1 Individual Assignment Foundations of Psychology Paper
- PAD540 Week9Assignment3
- Loot Crate Service*****Already A++ Rated Tutorial*****Use as Guide Paper*****
- Within your lists, consider and comment upon the developmental domains: physical, cognitive, and psychosocial.
- one page summary
- how you can become a better communicator