xlrd reading sheets from different files

986 views
Skip to first unread message

Josh English

unread,
Aug 12, 2013, 5:29:29 PM8/12/13
to python...@googlegroups.com
I have a class that reads two different Excel workbooks and ran into this bug.

SOURCE  = "path to an excel file"
TFXREF = "path to another excel file"


from xlrd import open_workbook

class Reader(object):
    def __init__(self):
        s = open_workbook(SOURCE)
        for sheet in s.sheets():
            print sheet

        t = open_workbook(TFXREF)
        print t.get_sheet(0)

r = Reader()

I run this code (with the proper paths built in) and get:

<xlrd.sheet.Sheet object at 0x05E52D10>
<xlrd.sheet.Sheet object at 0x05EDD670>
<xlrd.sheet.Sheet object at 0x05EF2630>
<xlrd.sheet.Sheet object at 0x05F45930>
Traceback (most recent call last):
  File "<string>", line 254, in run_nodebug
  File "C:\Users\josh\Desktop\xlrdbug.py", line 18, in <module>
    r = Reader()
  File "C:\Users\josh\Desktop\xlrdbug.py", line 14, in __init__
    print t.get_sheet(0)
  File "C:\Users\josh\Desktop\Portable Python\App\lib\site-packages\xlrd\book.py", line 682, in get_sheet
    raise XLRDError("Can't load sheets after releasing resources.")
xlrd.biffh.XLRDError: Can't load sheets after releasing resources.


The first book has four sheets. the second book only has one.

I can get around this by iterating through the sheets of the second book, but this seems silly when I know there is only one sheet.

I upgraded xlrd to 0.9.2 and still get this issue.

josh

John Machin

unread,
Aug 15, 2013, 7:35:55 AM8/15/13
to python...@googlegroups.com


On Tuesday, August 13, 2013 7:29:29 AM UTC+10, Josh English wrote:
I have a class that reads two different Excel workbooks and ran into this bug.

SOURCE  = "path to an excel file"
TFXREF = "path to another excel file"


from xlrd import open_workbook

class Reader(object):
    def __init__(self):
        s = open_workbook(SOURCE)
        for sheet in s.sheets():
            print sheet

        t = open_workbook(TFXREF)
        print t.get_sheet(0)

What gave you the impression that Book.get_sheet is a user-callable method? It's not mentioned in the docs.

Use Book.sheet_by_index
 

Josh English

unread,
Aug 15, 2013, 1:09:01 PM8/15/13
to python...@googlegroups.com


On Thursday, August 15, 2013 4:35:55 AM UTC-7, John Machin wrote:



What gave you the impression that Book.get_sheet is a user-callable method? It's not mentioned in the docs.

Use Book.sheet_by_index
 

I use PyScripter, with a handy popup, so when I type Book. the methods appear in a list, and "get_something" seems reasonable to me. If it had a leading underscore, I would not use it. I should code with the docs open in a second window, but I don't do that unless I'm learning something brand new to me, or debugging. It didn't even occur to me that "get_sheet" wouldn't be part of a public API.

Josh 
Reply all
Reply to author
Forward
0 new messages