How to read Excel files from a stream (and not a disk-support file) in Python?

Installed and tested XLRD:

>>> import xlrd
>>> workbook = xlrd.open_workbook('Sample.xls')

When I read the file through the html form as shown below, I can access all the values.

  xls_file = request.params['xls_file']
  print xls_file.filename, xls_file.type

I use the Pylons module, the request comes from: from pylons import request, tmpl_context as c

My questions:

  • Is the object xls_fileread requst.params?
  • How can I read xls_fileand make it work with xlrd?

Update:

xls_fileis uploaded to the web server, but the xlrd library expects a file name instead of an open file object. How to make the downloaded file work with xlrd? (Thanks to Martijn Pieters, I could not clearly state the question.)

+5
source share
2 answers

xlrd , file_contents:

xlrd.open_workbook(file_contents=fileobj.read())

:

file_contents. mmap.mmap - , . file_contents , filename , () .

+34

- ...

import xlrd

def newopen(fileobject, modes):
    return fileobject

oldopen = __builtins__.open
__builtins__.open = newopen
InputWorkBook = xlrd.open_workbook(fileobject)
__builtins__.open = oldopen

, StringIO, .

-2

All Articles