当前位置:   article > 正文

flask文件下载、数据存储到excel中_flask 导入excel

flask 导入excel

代码

# 下载接口
import pandas as pd
from flask import Flask, jsonify, request, redirect, render_template, send_from_directory
@app.route("/downLoad", methods=["GET"])
def downLoad():
    get_data = request.args.to_dict()
    id = get_data.get("id")
    details = InvoiceInformation.query.filter_by(fileId=id).all()
 
    data1 = []
    for i in details:
        returnJson = {}
        data_json = json.loads(json.dumps(i, cls=JSONEncoder))
        print(data_json['jsonStr'])
        json_str = eval(str(data_json['jsonStr']))
        if "发票代码" in json_str:
            returnJson['发票代码'] = json_str['发票代码']
        else:
            returnJson['是否通过'] = '未通过'
        if "发票号码" in json_str:
            returnJson['发票号码'] = json_str['发票号码']
        else:
            returnJson['是否通过'] = '未通过'
        if "税后价格" in json_str:
            returnJson['税后价格'] = json_str['税后价格']
        else:
            returnJson['是否通过'] = '未通过'
        if "开票日期" in json_str:
            returnJson['开票日期'] = json_str['开票日期']
        else:
            returnJson['是否通过'] = '未通过'
        if '是否通过' in returnJson:
            pass
        else:
            returnJson['是否通过'] = ' 通过'
        if data_json['validation'] == 1:
            returnJson['真伪验证'] = '真'
        else:
            returnJson['真伪验证'] = '假'
        print(i['detailedData'])
        returnJson['详情'] = str(i['detailedData'])
        data1.append(returnJson)

    df = pd.read_excel("1.xlsx", header=None)
    ds = pd.DataFrame(data1)
    df = df.append(ds, ignore_index=True)
    fileNumber = str(time.time()).replace(".", '')
    df.to_excel(fileNumber+".xlsx", index=False, header=True)
    return send_from_directory(directory, path=directory, filename=fileNumber+".xlsx", as_attachment=True)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

解读

data1 是我拼接的数据,如果没有什么意外情况,从数据库中查询出来,序列化一下,存储到list当中就可以直接追加进去
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/464537
推荐阅读
相关标签
  

闽ICP备14008679号