pythonencodingxlrdiunknown

python xlrd unknown encoding: unknown_codepage_10008


I am trying to open an Excel file (.xls) using xlrd. This is a summary of the code I am using:

def import_excel(key):
key_url = os.path.join(UPLOAD_DIR, key)
data = xlrd.open_workbook(key_url)
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols
colnames = table.row_values(0)
list = []
for rownum in range(1, nrows):
    row = table.row_values(rownum)
    if row:
        app = {}
        for i in range(len(colnames)):
            app[colnames[i]] = row[i]
        list.append(app)
return list

This works for most files, but fails for files I get from a specific organization. The error I get when I try to open Excel files from this organization follows.

ERROR *** codepage 10008 -> encoding 'unknown_codepage_10008' -> LookupError: unknown encoding: unknown_codepage_10008
Internal Server Error: /admin/result/detail/import/
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Library/Python/2.7/site-packages/django/utils/decorators.py", line 184, in inner
    return func(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/attackt/Desktop/jinji_workspace/grape/web/views/result_view.py", line 184, in detail_import
    item_list = import_excel(key)
  File "/Users/attackt/Desktop/jinji_workspace/grape/utils.py", line 45, in import_excel
    data = xlrd.open_workbook(key_url)
  File "/Library/Python/2.7/site-packages/xlrd/__init__.py", line 441, in open_workbook
    ragged_rows=ragged_rows,
  File "/Library/Python/2.7/site-packages/xlrd/book.py", line 116, in open_workbook_xls
    bk.parse_globals()
  File "/Library/Python/2.7/site-packages/xlrd/book.py", line 1170, in parse_globals
    self.handle_codepage(data)
  File "/Library/Python/2.7/site-packages/xlrd/book.py", line 794, in handle_codepage
    self.derive_encoding()
  File "/Library/Python/2.7/site-packages/xlrd/book.py", line 775, in derive_encoding
    _unused = unicode(b'trial', self.encoding)
LookupError: unknown encoding: unknown_codepage_10008

enter link description here

Link is an error file xls


Solution

  • Use for instance:.

    abook = xlrd.open_workbook(..., encoding_override="cp10008")