简体   繁体   English

旧的xls文件出现python xlrd错误

[英]python xlrd error with old xls file

I have got an excel file, that was created by some rather old soft. 我有一个excel文件,它是由一些较旧的软件创建的。 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. 此文件无法在OpenOffice中打开(某些编码错误),并且最初在Excel 2010中只能在受保护的视图中打开。 When I tried to open it by xlrd: 当我尝试通过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. 使用cp1252,utf-7编码的方法相同。 utf_16_le, that was adviced in similar topic returns utf_16_le,在类似主题返回中建议

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. 将文件保存为Excel 2010(xlsx)格式后,此问题消失了-可以同时在xlrd和OO中打开文件。 Is there any way to open such file by xlrd without resaving? 有什么方法可以通过xlrd打开此类文件而无需重新保存? Upd. UPD。 There is no such problem for python2.7 xlrd. python2.7 xlrd没有这样的问题。 However I still don't know what's wrong with python3.3 xlrd. 但是我仍然不知道python3.3 xlrd有什么问题。

The problem is in different behaviour between python2 and python3: 问题出在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()

To fix this you can edit xlrd/sheet.py around line 1543: 要解决此问题,您可以在1543行附近编辑xlrd / sheet.py:

Change 更改

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

to

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

So behaviour will be like in python2 因此行为将类似于python2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM