Python 3.x Openpyxl跳过for循环的迭代

Python 3.x Openpyxl跳过for循环的迭代,python-3.x,excel,openpyxl,Python 3.x,Excel,Openpyxl,我在python上使用openpyxl,我想对行求和,直到它们达到某个数字。在达到某个数字后,它会将总和粘贴到它旁边的单元格中。然后,循环将跳过所有已添加的行,继续到单元格并进行求和 要了解我正在尝试做什么: 从openpyxl导入加载\u工作簿 进口统计 wb=加载工作簿('Algorand.xlsx') 工作表=wb.active testlist=[] 对于列表中的行(sheet.columns)[0]: testlist=testlist+[row.value] 打印(测试列表) 平均值

我在python上使用openpyxl,我想对行求和,直到它们达到某个数字。在达到某个数字后,它会将总和粘贴到它旁边的单元格中。然后,循环将跳过所有已添加的行,继续到单元格并进行求和

要了解我正在尝试做什么:

从openpyxl导入加载\u工作簿
进口统计
wb=加载工作簿('Algorand.xlsx')
工作表=wb.active
testlist=[]
对于列表中的行(sheet.columns)[0]:
testlist=testlist+[row.value]
打印(测试列表)
平均值=统计平均值(测试列表)
std=statistics.stdev(测试列表)
目标=平均值+2*std
长度=长度(测试列表)
对于范围(0,长度-2)内的i:

如果testlist[i]您的代码似乎比获得您描述的结果所需的更复杂。只需循环遍历第一列中的单元格并将其添加到总数中即可。如果总数达到限制,则写入总数,然后将其重置为零

请尝试以下代码:

from openpyxl import load_workbook
summax = 12  # write sum if reached this total
wb=load_workbook('Algorand.xlsx')
sheet=wb.active

row = 1
ttl = 0
while bool(sheet.cell(row,1).value):  # while not empty cell
   ttl += int(sheet.cell(row,1).value)  # add to sum
   if ttl >= summax:  # if reached limit
      sheet.cell(row,2).value = ttl  # write sum
      ttl = 0   # reset sum
   row += 1  # next row

wb.save('Algorand.xlsx')
输出(限值=12)

from openpyxl import load_workbook
summax = 12  # write sum if reached this total
wb=load_workbook('Algorand.xlsx')
sheet=wb.active

row = 1
ttl = 0
while bool(sheet.cell(row,1).value):  # while not empty cell
   ttl += int(sheet.cell(row,1).value)  # add to sum
   if ttl >= summax:  # if reached limit
      sheet.cell(row,2).value = ttl  # write sum
      ttl = 0   # reset sum
   row += 1  # next row

wb.save('Algorand.xlsx')