convert dict to xlsx

This commit is contained in:
daovietanh99 2024-01-30 17:38:05 +07:00
parent 75819d9780
commit 34cd161c41
2 changed files with 77 additions and 55 deletions

View File

@ -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,67 +366,89 @@ def dict2xlsx(input: json):
wb = load_workbook(filename = 'report.xlsx')
ws = wb['Sheet1']
mapping = {
'A': 'subs',
'B': 'extraction_date',
'C': 'num_imei',
'D': 'num_invoice',
'E': 'total_images',
'F': 'images_quality.successful',
'G': 'images_quality.successful_percent',
'H': 'images_quality.bad',
'I': 'images_quality.bad_percent',
'J': 'average_accuracy_rate.imei',
'K': 'average_accuracy_rate.purchase_date',
'L': 'average_accuracy_rate.retailer_name',
'M': 'average_processing_time.imei',
'N': 'average_processing_time.invoice',
'O': 'usage.imei',
'P': 'usage.invoice',
}
if _type == 'report':
wb = load_workbook(filename = 'report.xlsx')
ws = wb['Sheet1']
mapping = {
'A': 'subs',
'B': 'extraction_date',
'C': 'num_imei',
'D': 'num_invoice',
'E': 'total_images',
'F': 'images_quality.successful',
'G': 'images_quality.successful_percent',
'H': 'images_quality.bad',
'I': 'images_quality.bad_percent',
'J': 'average_accuracy_rate.imei',
'K': 'average_accuracy_rate.purchase_date',
'L': 'average_accuracy_rate.retailer_name',
'M': 'average_processing_time.imei',
'N': 'average_processing_time.invoice',
'O': 'usage.imei',
'P': 'usage.invoice',
}
start_index = 5
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
ws[key + str(start_index)].fill = fill_gray
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
for record in subtotal['data']:
for key in mapping.keys():
value = get_value(record, mapping[key])
ws[key + str(start_index)] = value
if 'average_accuracy_rate' in mapping[key] and type(value) in [int, float] and value < 95:
ws[key + str(start_index)].style = normal_cell_red
elif 'average_processing_time' in mapping[key] and type(value) in [int, float] and value > 2.0:
ws[key + str(start_index)].style = normal_cell_red
elif 'bad_percent' in mapping[key] and type(value) in [int, float] and value > 10:
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])
ws[key + str(start_index)] = value
if 'average_accuracy_rate' in mapping[key] and type(value) in [int, float] and value < 95:
ws[key + str(start_index)].style = normal_cell_red
elif 'average_processing_time' in mapping[key] and type(value) in [int, float] and value > 2.0:
ws[key + str(start_index)].style = normal_cell_red
elif 'bad_percent' in mapping[key] and type(value) in [int, float] and value > 10:
ws[key + str(start_index)].style = normal_cell_red
else :
ws[key + str(start_index)].style = normal_cell
start_index += 1
return wb

Binary file not shown.