如何在Python中从流中读取Excel文件(而不是磁盘支持的文件)? [英] How to read Excel files from a stream (not a disk-backed file) in Python?
问题描述
XLRD已安装并经过测试:
XLRD is installed and tested:
>>> import xlrd
>>> workbook = xlrd.open_workbook('Sample.xls')
当我通过如下所示的html格式读取文件时,我可以访问所有值.
When I read the file through html form like below, I'm able to access all the values.
xls_file = request.params['xls_file']
print xls_file.filename, xls_file.type
我正在使用Pylons模块,请求来自:from pylons import request, tmpl_context as c
I'm using Pylons module, request comes from: from pylons import request, tmpl_context as c
我的问题:
-
xls_file
是通过requst.params
读取的对象吗? - 如何阅读
xls_file
并使其与xlrd兼容?
- Is
xls_file
read throughrequst.params
an object? - How can I read
xls_file
and make it work with xlrd?
更新:
xls_file
是在Web服务器上上传的,但是xlrd库需要文件名而不是打开的文件对象,如何使上载的文件与xlrd一起使用? (由于马丁·彼得斯(Martijn Pieters),我无法明确提出问题.)
The xls_file
is uploaded on web server, but the xlrd library expects a filename instead of an open file object, How can I make the uploaded file to work with xlrd? (Thanks to Martijn Pieters, I was being unable to formulate the question clearly.)
推荐答案
xlrd支持直接提供数据而无需文件路径,只需使用file_contents
自变量即可:
xlrd does support providing data directly without a filepath, just use the file_contents
argument:
xlrd.open_workbook(file_contents=fileobj.read())
从文档:
file_contents –字符串或
mmap.mmap
对象或某些其他行为类似的对象.如果提供了file_contents
,则将仅使用消息中的filename
.
file_contents – A string or an
mmap.mmap
object or some other behave-alike object. Iffile_contents
is supplied,filename
will not be used, except (possibly) in messages.
这篇关于如何在Python中从流中读取Excel文件(而不是磁盘支持的文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!