Question

I have got an excel file, that was created by some rather old soft. This file couldn't be opened in OpenOffice(some encoding errors) and in Excel 2010 at first it could only be opened in Protected View. When I tried to open it by xlrd:

from xlrd import open_workbook
rb = open_workbook('405_mut_1x.xls', encoding_override="utf-8")

I got an error:

Traceback (most recent call last):
  File "/home/wintr/PycharmProjects/4lab_1/main.py", line 2, in <module>
    rb = open_workbook('405_mut_1x.xls', encoding_override="utf-8")
  File "/usr/lib/python3/dist-packages/xlrd/__init__.py", line 435, in open_workbook
    ragged_rows=ragged_rows,
  File "/usr/lib/python3/dist-packages/xlrd/book.py", line 107, in open_workbook_xls
    bk.fake_globals_get_sheet()
  File "/usr/lib/python3/dist-packages/xlrd/book.py", line 714, in fake_globals_get_sheet
    self.get_sheets()
  File "/usr/lib/python3/dist-packages/xlrd/book.py", line 705, in get_sheets
    self.get_sheet(sheetno)
  File "/usr/lib/python3/dist-packages/xlrd/book.py", line 696, in get_sheet
    sh.read(self)
  File "/usr/lib/python3/dist-packages/xlrd/sheet.py", line 1467, in read
    self.update_cooked_mag_factors()
  File "/usr/lib/python3/dist-packages/xlrd/sheet.py", line 1535, in update_cooked_mag_factors
    elif not (10 <= zoom <= 400):
TypeError: unorderable types: int() <= NoneType()

Same thing with encoding by cp1252, utf-7. utf_16_le, that was adviced in similar topic returns

ERROR *** codepage None -> encoding 'utf_16_le' -> UnicodeDecodeError: 'utf16' codec can't decode byte 0x6c in position 4: truncated data

Without encoding I got additional string in traceback

*** No CODEPAGE record, no encoding_override: will use 'ascii'

After saving file in Excel 2010 (in xlsx) format this problem had disappeared - file can be opened both in xlrd and OO. Is there any way to open such file by xlrd without resaving? Upd. There is no such problem for python2.7 xlrd. However I still don't know what's wrong with python3.3 xlrd.

Was it helpful?

Solution

The problem is in different behaviour between python2 and python3:

$ python2
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 <= None
False

$ python3
Python 3.4.3 (default, Jul 28 2015, 18:20:59) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 <= None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() <= NoneType()

To fix this you can edit xlrd/sheet.py around line 1543:

Change

elif not (10 <= zoom <= 400):

to

elif zoom is None or not (10 <= zoom <= 400):

So behaviour will be like in python2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top