convert dict to xlsx
This commit is contained in:
parent
75819d9780
commit
34cd161c41
@ -346,7 +346,7 @@ def get_value(_dict, keys):
|
||||
return "-"
|
||||
|
||||
|
||||
def dict2xlsx(input: json):
|
||||
def dict2xlsx(input: json, _type='report'):
|
||||
red = "FF0000"
|
||||
black = "000000"
|
||||
green = "E2EFDA"
|
||||
@ -366,6 +366,9 @@ def dict2xlsx(input: json):
|
||||
wb = load_workbook(filename = 'report.xlsx')
|
||||
ws = wb['Sheet1']
|
||||
|
||||
if _type == 'report':
|
||||
wb = load_workbook(filename = 'report.xlsx')
|
||||
ws = wb['Sheet1']
|
||||
mapping = {
|
||||
'A': 'subs',
|
||||
'B': 'extraction_date',
|
||||
@ -384,37 +387,55 @@ def dict2xlsx(input: json):
|
||||
'O': 'usage.imei',
|
||||
'P': 'usage.invoice',
|
||||
}
|
||||
|
||||
start_index = 5
|
||||
|
||||
elif _type == 'report_detail':
|
||||
wb = load_workbook(filename = 'report_detail.xlsx')
|
||||
ws = wb['Sheet1']
|
||||
mapping = {
|
||||
'A': 'request_id',
|
||||
'B': 'redemption_number',
|
||||
'C': 'image_type',
|
||||
'D': 'imei_user_submitted',
|
||||
'E': "imei_ocr_retrieved",
|
||||
'F': "imei1_accuracy",
|
||||
'G': "purchase_date_user_submitted",
|
||||
'H': "purchase_date_ocr_retrieved",
|
||||
'I': "purchase_date_accuracy",
|
||||
'J': "retailer_user_submitted",
|
||||
'K': "retailer_ocr_retrieved",
|
||||
'L': "retailer_accuracy",
|
||||
'M': "average_accuracy",
|
||||
'N': "ocr_processing_time",
|
||||
'O': "is_reviewed",
|
||||
'P': "bad_image_reasons",
|
||||
'R': "countermeasures",
|
||||
}
|
||||
start_index = 4
|
||||
|
||||
for subtotal in input:
|
||||
ws['A' + str(start_index)] = subtotal['subs']
|
||||
ws['A' + str(start_index)].font = font_black
|
||||
ws['A' + str(start_index)].border = border
|
||||
ws['A' + str(start_index)].fill = fill_gray
|
||||
|
||||
ws['B' + str(start_index)] = subtotal['extraction_date']
|
||||
ws['B' + str(start_index)].font = font_black_bold
|
||||
ws['B' + str(start_index)].border = border
|
||||
ws['B' + str(start_index)].fill = fill_green
|
||||
|
||||
ws['C' + str(start_index)].border = border
|
||||
ws['D' + str(start_index)].border = border
|
||||
|
||||
for key in ['E', 'F', 'G', 'H', 'I']:
|
||||
ws[key + str(start_index)] = get_value(subtotal, mapping[key])
|
||||
ws[key + str(start_index)].font = font_black
|
||||
for key_index, key in enumerate(mapping.keys()):
|
||||
value = get_value(subtotal, mapping[key])
|
||||
ws[key + str(start_index)] = value
|
||||
ws[key + str(start_index)].border = border
|
||||
ws[key + str(start_index)].fill = fill_yellow
|
||||
|
||||
for key in ['J', 'K', 'L', 'M', 'N']:
|
||||
ws[key + str(start_index)] = get_value(subtotal, mapping[key])
|
||||
ws[key + str(start_index)].font = font_black
|
||||
ws[key + str(start_index)].border = border
|
||||
if _type == 'report':
|
||||
ws[key + str(start_index)].font = font_black_bold
|
||||
if key_index == 0 or (key_index >= 9 and key_index <= 15):
|
||||
ws[key + str(start_index)].fill = fill_gray
|
||||
elif key_index >= 4 and key_index <= 8:
|
||||
ws[key + str(start_index)].fill = fill_yellow
|
||||
elif _type == 'report_detail':
|
||||
if 'accuracy' in mapping[key] and type(value) in [int, float] and value < 95:
|
||||
ws[key + str(start_index)].style = normal_cell_red
|
||||
elif 'time' in mapping[key] and type(value) in [int, float] and value > 2.0:
|
||||
ws[key + str(start_index)].style = normal_cell_red
|
||||
else:
|
||||
ws[key + str(start_index)].style = normal_cell
|
||||
|
||||
start_index += 1
|
||||
|
||||
if 'data' in subtotal.keys():
|
||||
for record in subtotal['data']:
|
||||
for key in mapping.keys():
|
||||
value = get_value(record, mapping[key])
|
||||
@ -427,6 +448,7 @@ def dict2xlsx(input: json):
|
||||
ws[key + str(start_index)].style = normal_cell_red
|
||||
else :
|
||||
ws[key + str(start_index)].style = normal_cell
|
||||
|
||||
start_index += 1
|
||||
|
||||
return wb
|
||||
|
BIN
cope2n-api/report_detail.xlsx
Normal file
BIN
cope2n-api/report_detail.xlsx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user