瓶熊猫返回xls文件

时间:2017-04-13 12:06:37

标签: python pandas bottle

是否可以在瓶子中定义一个可以返回文件的路线?

  1. 我有一个mando数据库,可以通过pandas访问。
  2. Pandas根据请求参数生成xls文件。
  3. 上述两个步骤清晰易懂。 第三步是我遇到的问题。

    1. 定义一个瓶子路线,该路线将返回一个文件供用户下载。
    2. 我不想使用以前生成的静态文件。

      提前致谢。

1 个答案:

答案 0 :(得分:4)

我不熟悉Pandas,但您需要获取xls文件的二进制内容以通过Bottle路径发送给用户。来自{3}的Python 3修改示例:

from io import BytesIO
from bottle import route, response
from pandas import ExcelWriter


@route('/get-xlsx')
def get_xlsx():
    output = BytesIO()
    writer = ExcelWriter(output, engine='xlsxwriter')
    # Do something with your Pandas data
    # ...
    pandas_dataframe.to_excel(writer, sheet_name='Sheet1')
    writer.save()
    response.contet_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    response.add_header('Content-Disposition', 'attachment; filename="report.xlsx"')
    return output.getvalue()

当用户点击与此路线对应的链接时," report.xlxs"的文件下载对话框将在他们的浏览器中打开。

相关问题