Excel export with Flask server and xlsxwriter

后端 未结 3 1550
梦如初夏
梦如初夏 2021-02-01 09:41

So I\'ve been using XLSXWriter in the past to export an excel file containing one tab filled with two pandas dataframes. In the past I\'ve only been exporting the file to a loca

3条回答
  •  遇见更好的自我
    2021-02-01 10:15

    you can use something similar to this:

    from flask import Flask, send_file
    import io
    
    myio = io.StringIO()
    
    with open(xlsx_path, 'rb') as f:
        data = f.read()
    
    myio.write(data)
    myio.seek(0)
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        send_file(myio,
                  attachment_filename="test.xlsx",
                  as_attachment=True)
    
    app.run(debug=True)
    

    you may also want to write your excel file using tempfile

提交回复
热议问题