1. 程式人生 > >flask上傳excel檔案,無須儲存,直接讀取內容

flask上傳excel檔案,無須儲存,直接讀取內容

import xlrd
from flask import Flask, request


app = Flask(__name__)


@app.route("/", methods=['POST', 'GET'])
def filelist1():
    print(request.files)
    file = request.files['file']
    print('file', type(file), file)
    print(file.filename)    # 列印檔名

    f = file.stream.read()    #檔案內容
    data = xlrd.open_workbook(file_contents=f)
    table = data.sheets()[0]
    names = data.sheet_names()  # 返回book中所有工作表的名字
    status = data.sheet_loaded(names[0])  # 檢查sheet1是否匯入完畢
    print(status)
    nrows = table.nrows  # 獲取該sheet中的有效行數
    ncols = table.ncols  # 獲取該sheet中的有效列數
    print(nrows)
    print(ncols)
    s = table.col_values(0)  # 第1列資料
    for i in s:
        ii = i.strip()
        print(len(ii))
    return 'OK'



if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7000)