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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM