初学,搞不懂哪里出了问题

import openpyxl
from openpyxl import Workbook, load_workbook
wb = load_workbook('mmm.xlsx')
ws = wb.active
for j in range(2, 7):
    hSum = 0
  for i in range(2, 11):
        if ws.cell(i, 2).value == '小学':
            hSum=hSum+ws.cell(i, j + 1).value
#这句总是错误提示。unsupported operand type(s) for +: 'int' and 'NoneType'
        else:
            hSum = 0
  ws.cell(i, j + 1).value = hSum
讨论数量: 5

你把 hSum + ws.cell(i, j + 1).value 中的 ws.cell(i, j + 1).value 打印一下看看获取到的是什么值

3周前 评论
michalwei2022 (楼主) 3周前
zhaojjiang (作者) 3周前
Jason990420

The value of ws.cell(i, j + 1).value maybe not defined or None, so not support + with NoneType, like hSum + ws.cell(i, j + 1).value

If no value for a cell, you got None, check if the (row, col) is correct for the cells with values.

3周前 评论
pardon110

需要类型转换,表格无值给了None,数字与None类型不能直接运算

file

2周前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!