[Tutor] xlrd has no attribute

Roger Lea Scherer rls4jc at gmail.com
Thu Jan 11 17:52:50 EST 2018


I'm trying to learn how open a xls file in python. I thought this would be
easier. I've read on a blog this code and seen it on other websites. I
thought I'd try it out. I'm on Windows 10 with python 3.6.4.

import xlrd

def open_file(path):
    """
    Open and read an Excel file
    """
    book = xlrd.open_workbook(path)

    # print number of sheets
    print(book.nsheets)

    # print sheet names
    print(book.sheet_names())

    # get the first worksheet
    first_sheet = book.sheet_by_index(0)

    # read a row
    print(first_sheet.row_values(0))

    # read a cell
    cell = first_sheet.cell(0,0)
    print(cell)
    print(cell.value)

open_file("C:/Users/Roger/Documents/Roger/archive/area codes.xls")

But I get an error:

Traceback (most recent call last):
  File "C:\Users\Roger\Documents\Roger\Python\xlrd.py", line 1, in <module>
    import xlrd
  File "C:\Users\Roger\Documents\Roger\Python\xlrd.py", line 26, in <module>
    open_file("C:/Users/Roger/Documents/Roger/archive/area codes.xls")
  File "C:\Users\Roger\Documents\Roger\Python\xlrd.py", line 7, in open_file
    book = xlrd.open_workbook(path)
AttributeError: module 'xlrd' has no attribute 'open_workbook'

I've looked at http://www.lexicon.net/sjmachin/xlrd.html and it looks like
it should be ok. I've looked at Stack overflow and I have the
opening/closing parentheses appropriately placed to make it a method.

Can you help, please?


More information about the Tutor mailing list