如何在Python中从流中读取Excel文件(而不是磁盘支持的文件)? [英] How to read Excel files from a stream (not a disk-backed file) in Python?

查看:619
本文介绍了如何在Python中从流中读取Excel文件(而不是磁盘支持的文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的问题:

  1. xls_file是通过requst.params读取的对象吗?
  2. 如何阅读xls_file并使其与xlrd兼容?
  1. Is xls_file read through requst.params an object?
  2. 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. If file_contents is supplied, filename will not be used, except (possibly) in messages.

这篇关于如何在Python中从流中读取Excel文件(而不是磁盘支持的文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆