【问题标题】:Have Flask return a excel file in xlsx format as output [duplicate]让 Flask 返回 xlsx 格式的 excel 文件作为输出 [重复]
【发布时间】:2019-04-09 10:00:21
【问题描述】:

我有一个扩展名为 .xlsx 的 Excel 电子表格。我正在尝试将其作为我的 Flask 项目的一部分返回。

我正在尝试以下代码,但我不断收到错误

ValueError: View function did not return a response

代码如下:

file = pd.read_excel('output.xlsx')
writer = pd.ExcelWriter(file, engine='xlsxwriter')
resp = make_response(file.to_excel(writer))
resp.headers["Content-Disposition"] = "attachment; filename=output.xlsx"
resp.headers["Content-Type"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
return resp

【问题讨论】:

  • 你必须返回一个字符串或函数,你在这里试图返回一个对象
  • @min2bro 你能不能我只是想让 Excel 文件返回。我认为变量resp 会帮助我做到这一点。您能否就我在这方面的问题提出建议。谢谢。
  • 这可能会有所帮助:stackoverflow.com/questions/31391344/…

标签: python excel pandas flask


【解决方案1】:

您必须使用来自烧瓶库的send_file

这是一个例子:

from flask import send_file
@routes.route("/files/download", methods=['GET'])
def download():
    file_path = '/your/file/path'
    return send_file(
        file_path,
        mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        as_attachment=True)

【讨论】:

  • 谢谢你的帮助..
猜你喜欢
  • 2010-09-13
  • 2020-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多