You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
7.0 KiB

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Date:2022/07/25 14:57:23
'''
import time
import json
import uuid
import datetime
from string import ascii_uppercase
from openpyxl import load_workbook
2 years ago
class ReadExcel:
def __init__(self, excelName):
2 years ago
self.wb = load_workbook(filename=excelName)
self.ws = self.wb.active
self.template_list = []
def read(self):
max_lines = self.ws.max_row
if self.ws['C1'].value == 'template_content':
for key in range(2, max_lines + 1):
value = self.ws['{}'.format('C' + str(key))].value
self.template_list.append(value)
return self.template_list
elif self.ws['A1'].value == '申购单编号' and self.ws['B1'].value == '采购单编号':
drug_list = []
# lineIndex = 0
try:
for line in range(2, max_lines + 1):
2 years ago
# 局部方法,传递字符和默认值返回方法调用,简化代码量
2 years ago
value_func = lambda x, y='': str(
self.ws[f"{x}{line}"].value or y)
# 声明一个字典存放每一行的数据 每遍历一次字典重置一次
drug_template_dict = {
"variety_id": str(uuid.uuid1()),
}
key_list = [
2 years ago
"name", "english_name", "purity", "cas_number", "speci", "net_weight_unit", "export_count", "export_unit",
"production_date", "shelf_life", "expiration_date", "price", "is_supervise", "total", "manufacturer", "remain"
]
# 采用列表和字母对应的两个列表,进行循环的方式写入字典,简化代码量
2 years ago
v_list = list("EFGHJKLMNOPQRSTU")
str_tim = "%Y-%m-%d %H:%M:%S"
def date_func(x): return datetime.datetime.strptime(
x, str_tim)
for i in range(len(key_list)):
2 years ago
v_key = v_list[i]
key_key = key_list[i]
# 时间格式转换
if key_key == "production_date" or key_key == "expiration_date":
value = date_func(value_func(
v_key)).strftime(str_tim)
elif key_key == "is_supervise":
va = value_func(v_key)
if va == "":
value = 0
else:
value = 1
elif key_key == "remain":
value = drug_template_dict.get("speci")
else:
value = value_func(v_key)
drug_template_dict[key_list[i]] = value
if key_list[i] == "name" and drug_template_dict.get("name") == "":
self.template_list.append(json.dumps(drug_list))
return self.template_list
2 years ago
# 时间判断逻辑较为凌乱,采用原思路进行重写
# 定义当前时间对象
2 years ago
# pDate = datetime.datetime.now()
# 局部性时间字符串,共用
2 years ago
# 定义公共方法,处理时间转换
# 和原逻辑一样,简化写法
2 years ago
# if not value_func("P"):
# if value_func("O"):
# pDate = date_func(value_func("N"))
# shelf_life = value_func("O", "3650")
# else:
# if value_func("N"):
# pDate = date_func(value_func('N'), str_tim)
# shelf_life = self.time_long(
# date_func(pDate.strftime(str_tim)), date_func(value_func("P")))
# drug_template_dict["production_date"] = pDate.strftime(
# str_tim)
# drug_template_dict["shelf_life"] = shelf_life
# if(not self.ws['P{}'.format(str(line))].value):
# pDate = datetime.datetime.now()
# if(self.ws['O{}'.format(str(line))].value):
# pDate = datetime.datetime.strptime(str(self.ws['N{}'.format(str(line))].value), "%Y-%m-%d %H:%M:%S")
# drug_template_dict['ProductionDate'] = pDate.strftime("%Y-%m-%d")
# drug_template_dict['ShelfLife'] = str(self.ws['O{}'.format(str(line))].value or '3650')
# else:
# pDate = datetime.datetime.now()
# if(self.ws['N{}'.format(str(line))].value):
# pDate = datetime.datetime.strptime(str(self.ws['N{}'.format(str(line))].value), "%Y-%m-%d %H:%M:%S")
# drug_template_dict['ProductionDate'] = pDate.strftime("%Y-%m-%d")
# drug_template_dict['ShelfLife'] = self.time_long(
# datetime.datetime.strptime(pDate.strftime("%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S"), datetime.datetime.strptime(str(self.ws['P{}'.format(str(line))].value), "%Y-%m-%d %H:%M:%S"))
2 years ago
# v_list1 = list("QRSTUVW")
# key_list1 = [
# "price", "is_supervise", "remain", "manufacturer", "remark8", "remark9", "remark10"
# ]
# for x in range(len(key_list1)):
# ex = ''
# if key_list1[x] in ["price", "remain"]:
# ex = "0"
# val = value_func(v_list1[x], ex)
# if v_list1[x] == 'R':
# if val != "是":
# value = 0
# else:
# value = 1
# else:
# value = val
# drug_template_dict[key_list1[x]] = value
drug_list.append(drug_template_dict)
self.template_list.append(json.dumps(drug_list))
return self.template_list
except Exception as e:
raise RuntimeError('{0}行;{1}'.format(str(line), str(e)))
else:
raise RuntimeError('模板格式错误!')
def close(self):
self.wb.close()
def time_long(self, time1, time2, type="day"):
"""
计算时间差
:param time1: 较小的时间datetime类型
:param time2: 较大的时间datetime类型
:param type: 返回结果的时间类型暂时就是返回相差天数
:return: 相差的天数
"""
2 years ago
def da(x): return time.strptime(str(x), '%Y-%m-%d %H:%M:%S')
if type == 'day':
day_num = (int(time.mktime(da(time2))) - int(time.mktime(da(time1)))) / (
24 * 60 * 60)
return abs(int(day_num))