First, get a list of the names of all available worksheets:
worksheet_list = wb.sheetnames
Then analyze each worksheet to identify its structure and learn where to find the column with the caloric intake. You will have to iterate over the list of sheetnames and create two variables – one to hold the selected worksheet, and the second for the total caloric input:
for worksheet in worksheet_list:
selected_ws = wb[worksheet]
caloric_sum = 0
Next, create an inner loop to iterate over the selected columns/rows from a given worksheet:
for row in selected_ws.iter_rows(min_row=2, ...):
for cell in row:
caloric_sum += cell.value
Remember that you should set the max_row
of the iter_rows
function to the max row of that sheet, as the number of rows differs in each sheet.
After the loops end, use the print()
function:
print(selected_ws.title, ":", caloric_sum, "kcal")