flask上傳excel文件,無須存儲,直接讀取內容

運行環境python3.6

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.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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章