与旧的xls文件的python xlrd错误

时间:2014-05-09 08:11:58

标签: python excel xlrd

我有一个excel文件,它是由一些相当古老的软件创建的。此文件无法在OpenOffice中打开(某些编码错误),而在Excel 2010中,它首先只能在受保护的视图中打开。 当我试图通过xlrd打开它时:

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

我收到了一个错误:

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()

与cp1252,utf-7编码相同。 utf_16_le,在类似主题中被提出建议

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

没有编码我在追溯中获得了额外的字符串

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

在Excel 2010(xlsx)格式中保存文件后,此问题已消失 - 可以在xlrd和OO中打开文件。有没有办法在没有重新启动的情况下通过xlrd打开这样的文件? UPD。 python2.7 xlrd没有这样的问题。但是我仍然不知道python3.3 xlrd有什么问题。

1 个答案:

答案 0 :(得分:0)

问题在于python2和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()

要解决此问题,您可以在第1543行编辑xlrd / sheet.py:

更改

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

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

所以行为就像在python2中一样