私はPandasに慣れていませんが、ボトルルート経由でユーザーに送信するにはxlsファイルのバイナリコンテンツを取得する必要があります。 Pythonの3のためにhereから変形例:ユーザーはこのルートに対応するリンクをクリックすると
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」のファイルダウンロードダイアログがブラウザで開きます。
どうもありがとう。 BytesIOについてもう少し読んでみましょう。 –
BytesIOは、メモリ内のファイルライクなオブジェクトです。 WSGIプロトコルはファイルのような反復可能なオブジェクトをペイロードとして受け入れるので、getvalue()を使わずに直接返すこともできます。 –