Re: Error message when xlrd.open_workbook('book1.xls') is called

2,921 views
Skip to first unread message

John Machin

unread,
Mar 12, 2013, 3:19:15 PM3/12/13
to python-excel


On Mar 13, 4:39 am, Harrington <hri...@gmail.com> wrote:
> >>> import xlrd
> >>> wb = xlrd.open_workbook('Book1.xls')
> Traceback (most recent call last):
>   File "<pyshell#12>", line 1, in <module>
>     wb = xlrd.open_workbook('Book1.xls')
>   File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 425, in open_workbook
>     on_demand=on_demand,
>   File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 878, in biff2_8_load
>     f = open(filename, open_mode)
> IOError: [Errno 2] No such file or directory: 'Book1.xls

When you start IDLE, the current working directory is set to (in your
case) C:\Python27. Keeping data files in a version-specific software
installation directory is not a good idea, and your files are
elsewhere e.g. C:\Harrington\data.

You have two choices:

(1) Always specify the absolute path:

import xlrd, os.path
data_folder = r"c:\Harrington\data"
filename = r"meaningful_name.xls"
wb = xlrd.open_workbook(os.path.join(data_folder, filename))

(2) change the current working directory:

import xlrd, os
data_folder = r"c:\Harrington\data"
os.chdir(data_folder)
filename = r"meaningful_name.xls"
wb = xlrd.open_workbook(filename)

Note: none of the above is specific to xlrd. As you can see from the
traceback, xlrd merely calls the built-in open function with whatever
path you supply.

Harrington

unread,
Apr 2, 2013, 10:57:07 PM4/2/13
to python...@googlegroups.com
  This is what i got after trying out the following as in suggestion 1. listed below

>>> import xlrd,os
>>> data_folder = "C://Python33//Lib//site-packages//xlrd"
>>> filename = "book1.xlsx"
>>> wb = xlrd.open_workbook(os.path.join(data_folder,filename))


Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    wb = xlrd.open_workbook(os.path.join(data_folder,filename))
  File "C:\Python25\lib\site-packages\xlrd\__init__.py", line 429, in open_workbook
    biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
  File "C:\Python25\lib\site-packages\xlrd\__init__.py", line 1545, in getbof
    bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])
  File "C:\Python25\lib\site-packages\xlrd\__init__.py", line 1539, in bof_error
    raise XLRDError('Unsupported format, or corrupt file: ' + msg)
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found 'PK\x03\x04\x14\x00\x06\x00'
>>>


On Tuesday, March 12, 2013 10:39:36 AM UTC-7, Harrington wrote:
 
 Hi
 I'm new to this forum,however I want to  Read data from excel  into python where data will be manipulated using  xlrd . I'm using python 2.7 ,xlrd built fine, with out any problem. when I import xlrd and tries calling  book1.xls, get the follow exceptions thrown at me :



>>> import xlrd
>>> wb = xlrd.open_workbook('Book1.xls')

Traceback (most recent call last):

  File "<pyshell#12>", line 1, in <module>

    wb = xlrd.open_workbook('Book1.xls')

  File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 425, in open_workbook
    on_demand=on_demand,

  File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 878, in biff2_8_load
    f = open(filename, open_mode)

IOError: [Errno 2] No such file or directory: 'Book1.xls



 Sure there is a problem could someone point me in the right direction, will appreciate.
 Thanks

John Machin

unread,
Apr 3, 2013, 5:59:50 AM4/3/13
to python-excel


On Apr 3, 1:57 pm, Harrington <hri...@gmail.com> wrote:
>   This is what i got after trying out the following as in suggestion 1.
> listed below
>
> >>> import xlrd,os
> >>> data_folder = "C://Python33//Lib//site-packages//xlrd"

That's about the last place you should put your data.. Keep data and
software in totally separate folders.

> >>> filename = "book1.xlsx"
> >>> wb = xlrd.open_workbook(os.path.join(data_folder,filename))
>
> Traceback (most recent call last):
>   File "<pyshell#32>", line 1, in <module>
>     wb = xlrd.open_workbook(os.path.join(data_folder,filename))
>   File "C:\Python25\lib\site-packages\xlrd\__init__.py", line 429, in
> open_workbook
>     biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
>   File "C:\Python25\lib\site-packages\xlrd\__init__.py", line 1545, in
> getbof
>     bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])
>   File "C:\Python25\lib\site-packages\xlrd\__init__.py", line 1539, in
> bof_error
>     raise XLRDError('Unsupported format, or corrupt file: ' + msg)
> XLRDError: Unsupported format, or corrupt file: Expected BOF record; found
> 'PK\x03\x04\x14\x00\x06\x00'

That indicates they you are using Python 0.7.x or earlier -- doesn't
handle XLSX files.

; Get either version 0.8.0 from pypi or the zip version from github
trunk -- don't get the 0.9.0 from pypi; it has bugs which are fixed in
the github trunk version.

John McNamara

unread,
Apr 3, 2013, 5:59:56 AM4/3/13
to python...@googlegroups.com


On Wednesday, 3 April 2013 03:57:07 UTC+1, Harrington wrote:
  This is what i got after trying out the following as in suggestion 1. listed below

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found 'PK\x03\x04\x14\x00\x06\x00'
>>>


Hi,

That is the file signature for an XLSX file not an XLS file. However, recent versions of xlrd should handle those as well.

Are you using the latest version of xlrd?

John


 

Anurag Chourasia

unread,
Apr 3, 2013, 8:14:52 AM4/3/13
to python...@googlegroups.com

You might want to upgrade to xlrd 0.8.0 and above.  .  . That will solve this issue with xlsx file as the support was added In that version.  Latest at the moment is 0.9.0 so even better to use that version.

Regards,
Anurag

On 12 Mar 2013 14:41, "Harrington" <hri...@gmail.com> wrote:
 
 Hi
 I'm new to this forum,however I want to  Read data from excel  into python where data will be manipulated using  xlrd . I'm using python 2.7 ,xlrd built fine, with out any problem. when I import xlrd and tries calling  book1.xls, get the follow exceptions thrown at me :


>>> import xlrd
>>> wb = xlrd.open_workbook('Book1.xls')

Traceback (most recent call last):

  File "<pyshell#12>", line 1, in <module>

    wb = xlrd.open_workbook('Book1.xls')

  File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 425, in open_workbook
    on_demand=on_demand,

  File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 878, in biff2_8_load
    f = open(filename, open_mode)

IOError: [Errno 2] No such file or directory: 'Book1.xls



 Sure there is a problem could someone point me in the right direction, will appreciate.
 Thanks

--
You received this message because you are subscribed to the Google Groups "python-excel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-excel...@googlegroups.com.
To post to this group, send an email to python...@googlegroups.com.
Visit this group at http://groups.google.com/group/python-excel?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Harrington

unread,
May 6, 2013, 9:49:57 AM5/6/13
to python...@googlegroups.com
  hello thanks for  helping me out on this issue. problem was with the installation. it is very helpful to use Easy_Install to do all initial installation.
Reply all
Reply to author
Forward
0 new messages