python - Flask App outputting Excel file -
here's have... application outputting excel file, don't know how output excel file has data in it. right it's outputting excel file contains "test" in cell a1, , know that's because of line says strio.write('test')
. how can make sure file outputs download user contains of information file processed?
thanks.
from openpyxl import load_workbook flask import flask, request, render_template, redirect, url_for, send_file import stringio app = flask(__name__) @app.route('/') def index(): return """<title>upload new file</title> <h1>upload new file</h1> <form action="/uploader" method=post enctype=multipart/form-data> <p><input type=file name=file> <input type=submit value=upload> </form>""" @app.route('/uploader', methods = ['get', 'post']) def upload(): if request.method == 'post': f = request.files['file'] f.save(f.filename) return process(f.filename) def process(filename): routename = ['zyaa', 'zybb', 'zycc'] supervisors = ['x', 'y', 'z'] workbook = load_workbook(filename) worksheet = workbook.active worksheet.column_dimensions.group('a', 'b', hidden=true) routes = worksheet.columns[2] = 2 worksheet['d1'] = 'supervisor' route in routes: if route.value in routename: pos = routes.index(route) worksheet['d' + str(i)].value = supervisors[pos] += 1 workbook.save(filename) filename = filename.strip(".xlsx") filename = filename + ".xls" strio = stringio.stringio() strio.write('test') strio.seek(0) return send_file(strio, attachment_filename=filename, as_attachment=true) if __name__ == '__main__': app.run(debug = true, host = '0.0.0.0')
the send_file
function in flask sends file specifying in first argument (compare documentation). put there strio
means string saved there send. if want send file prepering before, should put file there, i.e.
send_file(filename, attachment_filename=filename, as_attachment=true)
Comments
Post a Comment