Return Excel Flask 中的文件
Return Excel file in Flask
我正在 python 中构建一个烧瓶应用程序 3. 我正在尝试写入输出并响应下载。我所做的就是将 sqlite3 数据库内容写入一个 excel 文件,试图发送到客户端进行下载。在创建 excel 文件之前,一切似乎都运行良好。我无法发送给客户。
@app.route('/download', methods=['GET'])
def export_db():
values = execute("SELECT * from table",[])
wb = Workbook()
ws = wb.active
for item in values.fetchall():
ws.append(item)
wb.save('example.xlsx')
output = make_response(wb)
output.headers["Content-Disposition"] = "attachment; filename=" +
"example.xlsx"
output.headers["Content-type"] = "application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet"
return output
我收到的错误信息是,
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1612,
in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1598,
in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/user/Documents/main.py",
line 218, in export_db
output = make_response(wb)
File "/usr/local/lib/python3.5/dist-packages/flask/helpers.py", line
191, in make_response
return current_app.make_response(args)
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1740,
in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/usr/local/lib/python3.5/dist-packages/werkzeug/wrappers.py",
line 911, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/usr/local/lib/python3.5/dist-packages/werkzeug/wrappers.py",
line 59, in _run_wsgi_app
return _run_wsgi_app(*args)
File "/usr/local/lib/python3.5/dist-packages/werkzeug/test.py", line
884, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'Workbook' object is not callable
我已经提到了几个类似的问题,但其中 none 似乎有帮助。任何帮助将不胜感激。
注意:
当我尝试将文件路径添加到 make_response 时,它将文件路径作为字符串,而 returns 我将文件路径作为内容的 .xlsx 文件。知道我做错了什么吗?
像这样的东西应该可以工作:
from flask import send_file
from xlsxwriter import Workbook
@app.route('/download', methods=['GET'])
def export_db():
values = execute("SELECT * from table",[])
wb = Workbook('path/to/workbook.xlsx')
wb.add_worksheet('All Data')
for item in values.fetchall():
wb.write(item)
wb.close()
return send_file('path/to/workbook.xlsx')
我正在 python 中构建一个烧瓶应用程序 3. 我正在尝试写入输出并响应下载。我所做的就是将 sqlite3 数据库内容写入一个 excel 文件,试图发送到客户端进行下载。在创建 excel 文件之前,一切似乎都运行良好。我无法发送给客户。
@app.route('/download', methods=['GET'])
def export_db():
values = execute("SELECT * from table",[])
wb = Workbook()
ws = wb.active
for item in values.fetchall():
ws.append(item)
wb.save('example.xlsx')
output = make_response(wb)
output.headers["Content-Disposition"] = "attachment; filename=" +
"example.xlsx"
output.headers["Content-type"] = "application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet"
return output
我收到的错误信息是,
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1612,
in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1598,
in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/user/Documents/main.py",
line 218, in export_db
output = make_response(wb)
File "/usr/local/lib/python3.5/dist-packages/flask/helpers.py", line
191, in make_response
return current_app.make_response(args)
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1740,
in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/usr/local/lib/python3.5/dist-packages/werkzeug/wrappers.py",
line 911, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/usr/local/lib/python3.5/dist-packages/werkzeug/wrappers.py",
line 59, in _run_wsgi_app
return _run_wsgi_app(*args)
File "/usr/local/lib/python3.5/dist-packages/werkzeug/test.py", line
884, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'Workbook' object is not callable
我已经提到了几个类似的问题,但其中 none 似乎有帮助。任何帮助将不胜感激。
注意: 当我尝试将文件路径添加到 make_response 时,它将文件路径作为字符串,而 returns 我将文件路径作为内容的 .xlsx 文件。知道我做错了什么吗?
像这样的东西应该可以工作:
from flask import send_file
from xlsxwriter import Workbook
@app.route('/download', methods=['GET'])
def export_db():
values = execute("SELECT * from table",[])
wb = Workbook('path/to/workbook.xlsx')
wb.add_worksheet('All Data')
for item in values.fetchall():
wb.write(item)
wb.close()
return send_file('path/to/workbook.xlsx')