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

时间:2018-07-05 04:48:21

标签: openpyxl

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')

1 个答案:

答案 0 :(得分:1)

#! 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的东西。简单的错误,但除此之外,您的代码还可以。