python - Excel export with Flask server and xlsxwriter -


so i've been using xlsxwriter in past export excel file containing 1 tab filled 2 pandas dataframes. in past i've been exporting file local path on user's computer i'm doing transition web interface.

my desired output have same excel file code below, created in memory , sent user him/her download through web interface. i've been seeing lot of django , stringio i'm looking work flask , not find worked.

is familiar problem?

thanks in advance!

xlsx_path = "c:\test.xlsx" writer = pd.excelwriter(xlsx_path, engine='xlsxwriter')  df_1.to_excel(writer,startrow = 0, merge_cells = false, sheet_name = "sheet_1") df_2.to_excel(writer,startrow = len(df_1) + 4, merge_cells = false , sheet_name = "sheet_1")                               workbook = writer.book worksheet = writer.sheets["sheet_1"] format = workbook.add_format() format.set_bg_color('#eeeeee') worksheet.set_column(0,9,28)  writer.close() 

the following snippet works on win10 python 3.4 64bit.

the pandas excelwriter writes bytesio stream sent user via flask , send_file.

import numpy np import pandas pd io import bytesio flask import flask, send_file  app = flask(__name__) @app.route('/')  def index():      #create random pandas dataframe     df_1 = pd.dataframe(np.random.randint(0,10,size=(10, 4)), columns=list('abcd'))      #create output stream     output = bytesio()     writer = pd.excelwriter(output, engine='xlsxwriter')      #taken original question     df_1.to_excel(writer, startrow = 0, merge_cells = false, sheet_name = "sheet_1")     workbook = writer.book     worksheet = writer.sheets["sheet_1"]     format = workbook.add_format()     format.set_bg_color('#eeeeee')     worksheet.set_column(0,9,28)      #the writer has done job     writer.close()      #go beginning of stream     output.seek(0)      #finally return file     return send_file(output, attachment_filename="testing.xlsx", as_attachment=true)  app.run(debug=true) 

references:


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -