'NoneType' 对象没有属性 'max_row'

'NoneType' object has no attribute 'max_row'

os whidow7
python3.6
openpyxl 2.5.4
------------------------------------------------------------------
how to solve this problem?

Traceback (most recent call last):
  File "C:\python\updataProduce.py", line 22, in 
    countRow = sheet.max_row()
AttributeError: 'NoneType' object has no attribute 'max_row'

-----------------------------------------------------------
#! python3
# updataProduce.py - corrects costs in produce sales spreadsheet

from openpyxl import Workbook
wb = Workbook('produceSales.xlsx')
sheet = wb.active

# The produce types and their updated price
PRICE_UPDATES = {'Garlic': 3.07, 'Celery': 1.19, 'Lemon':1.27}

# TODO: loop through the rows and update the PRICE
countRow = sheet.max_row()
print(countRow)
for rowNum in range(2, countRow) :
    produceName = sheet.cell(row = rowNum, column = 1).value
    if produceName in PRICE_UPDATES:
        sheet.cell(row = rowNum, column = 2).value = PRICE_UPDATES[produceName]
wb.save('updateProduceSales.xlsx')
#! python3
# updataProduce.py - corrects costs in produce sales spreadsheet

from openpyxl import Workbook
wb = Workbook('produceSales.xlsx')
sheet = wb.active

# The produce types and their updated price
PRICE_UPDATES = {'Garlic': 3.07, 'Celery': 1.19, 'Lemon':1.27}

# TODO: loop through the rows and update the PRICE
countRow = sheet.max_row
print(countRow)
for rowNum in range(2, countRow) :
    produceName = sheet.cell(row = rowNum, column = 1).value
    if produceName in PRICE_UPDATES:
        sheet.cell(row = rowNum, column = 2).value = PRICE_UPDATES[produceName]
wb.save('updateProduceSales.xlsx')

这应该对你有用了。错误消息解释了问题所在:

countRow = sheet.max_row() AttributeError: 'NoneType' object has no attribute 'max_row'

是说对象没有属性叫max_row()。但是它确实有一个叫做 max_row。一个简单的错误,但除此之外你的代码还不错。