在Python烧瓶中上传,读取,写入excel文件

时间:2017-10-25 06:36:20

标签: python excel flask

我正在使用此代码,要求用户上传一个文件,我希望将其读入数据框。 然后,此数据框应显示为页面上的输出。

我应该在回报中写些什么来完成这个?

from flask import Flask, request, jsonify
import flask_excel as excel
import pandas as pd

app=Flask(__name__)

@app.route("/upload", methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        return jsonify({"result": request.get_array(field_name='file')})
    return '''
    <!doctype html>
    <title>Upload an excel file</title>
    <h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1>
    <form action="" method=post enctype=multipart/form-data>
    <p><input type=file name=file><input type=submit value=Upload>
    </form>
    '''

@app.route("/export", methods=['GET'])
def export_records():
    return 

if __name__ == "__main__":
    app.run()

1 个答案:

答案 0 :(得分:5)

我想你想要的准系统就是这个。但这显然需要更多的工作。

from flask import Flask, request, jsonify
import pandas as pd

app=Flask(__name__)

@app.route("/upload", methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        print(request.files['file'])
        f = request.files['file']
        data_xls = pd.read_excel(f)
        return data_xls.to_html()
    return '''
    <!doctype html>
    <title>Upload an excel file</title>
    <h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1>
    <form action="" method=post enctype=multipart/form-data>
    <p><input type=file name=file><input type=submit value=Upload>
    </form>
    '''

@app.route("/export", methods=['GET'])
def export_records():
    return 

if __name__ == "__main__":
    app.run()