简体   繁体   English

Flask 上传读取 xlsx 文件而不保存它

[英]Flask Uploads reading an xlsx file without saving it

I'd like to upload an excel file in my web app, read the contents of it and display some cells.我想在我的网络应用程序中上传一个 excel 文件,阅读其中的内容并显示一些单元格。 So basically I don't need to save the file as it's a waste of time.所以基本上我不需要保存文件,因为这是浪费时间。

Relevant code:相关代码:

if form.validate_on_submit():

        f = form.xml_file.data.stream

        xml = f.read()

        workbook = xlrd.open_workbook(xml)

        sheet = workbook.sheet_by_index(0)

I can't wrap my mind around this as I keep getting filetype errors no matter what I try.我无法解决这个问题,因为无论我尝试什么,我都会不断收到文件类型错误。 I'm using Flask Uploads, WTF.file and xlrd for reading the file.我正在使用 Flask Uploads、WTF.file 和 xlrd 来读取文件。

Reading the file works okay if I save it previously with f.save如果我之前使用 f.save 保存文件,则读取文件可以正常工作

To answer my own question, I solved it with为了回答我自己的问题,我解决了

if form.validate_on_submit():

        # Put the file object(stream) into a var
        xls_object = form.xml_file.data.stream

        # Open it as a workbook
        workbook = xlrd.open_workbook(file_contents=xls_object.read())

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

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