Python/Excel处理/main.py

73 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
就excel工作表进行处理包括就指定字段解析JSON
"""
from json import loads
from pandas import DataFrame, read_excel
from utils.pandas_extension import save_as_workbook
# 打开并读取指定工作表(默认以字符串读取)
sheet = read_excel(io="dataset.xlsx", sheet_name="Sheet1", dtype=str)
dataset = []
for index, row in sheet.iterrows():
# 就指定字段解析为JSON
response = loads(row["response"])
data = {}
# 根据深圳快瞳票据查验要求解析查验结果
# 状态码
status = response.get("status", "")
# 错误码
code = response.get("code", "")
# 若状态码为200且错误码为10000则定义为响应成功
if status == 200 and code == 10000:
# 查验类型若查验类型为003081则为医疗收费票据=003082则为增值税发票
match response.get("data").get(
"productCode"
):
# 解析医疗收费票据
case "003081":
data["发票号"] = response.get("data").get(
"billNumber"
)
# 查验结果
match response.get("data").get("flushedRed"):
case "true":
data["查验结果"] = "正常"
case "false":
data["查验结果"] = "已红冲"
# 解析增值税发票
case "003082":
data["发票号"] = (
response.get("data").get("details").get("number")
)
# 查验结果
match response.get("data").get("details").get(
"invoiceTypeNo"
):
case "0":
data["查验结果"] = "正常"
case "1":
data["查验结果"] = "无法查验"
case "2" | "3" | "7" | "8":
data["查验结果"] = "已红冲"
# 若状态码为400且错误码为10001或10100则定义为假票
elif status == 400 and (code == 10001 or code == 10100):
data["查验结果"] = "假票"
else:
data["查验结果"] = "无法查验"
dataset.append(data)
# 本地保存
save_as_workbook(worksheets=[("Sheet1", DataFrame(data=dataset, dtype=str))], workbook_name="results.xlsx")