I have an excel spreadsheet whose data are as follows (from A1 to C3): original spreadsheet I want to calculate the chances of none-zero values in a column, then write the result to the last cell in this column. In the case of the third column, the result should be 2/3 = 0.67 Below is the Python script that I wrote to do the same thing, but it gets the wrong result obviously.
The code:
import openpyxl
wb = openpyxl.load_workbook('testXls.xlsx')
sheet = wb.active
for colNum in range(1, sheet.max_column + 1):
coverCount = 0
for rowNum in range(1, sheet.max_row + 1):
if sheet.cell(row=rowNum, column=colNum).value != 0:
coverCount += 1
sheet.cell(row=4, column=colNum).value = round(coverCount / 3, 2)
wb.save('testXls2.xlsx')
The result: result spreadsheet
I can't find anything wrong in the code. Could someone enlighten me on this please? I really appreciate it.