Search code examples
pythonxlsxxlrd

AttributeError: 'NoneType' object has no attribute 'nrows'


I'm going to use nrows for my xlsx file but I get this error: NoneType' object has no attribute 'nrows'

Yesterday, it was working but when I rewrote my program this error showed up.

My code is:

import xlrd
def read_excel_files(loc):
    file=xlrd.open_workbook(loc)
    sheet=file.sheet_by_index(0)
    
sheet=read_excel_files("C:/Users/****/Desktop/DataSet.xlsx")
dataset=[]
    
for i in range(0 , sheet.nrows):
    temp_list=[sheet.cell.value(i , 0), sheet.cell.value(i , 0)]
    dataset.append(temp_list)
    print(dataset)

Solution

  • as William said that returning the variable sheet will solve the problem, here is your modified code:

    import xlrd
    def read_excel_files(loc):
        file=xlrd.open_workbook(loc)
        sheet=file.sheet_by_index(0)
        return sheet
    
    sheet=read_excel_files("C:/Users/****/Desktop/DataSet.xlsx")
    dataset=[]
    
    for i in range(0 , sheet.nrows):
        temp_list=[sheet.cell.value(i , 0), sheet.cell.value(i , 0)]
        dataset.append(temp_list)
        print(dataset)