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

Binary file not shown.