From 34cd161c41f4135181212b3bd9608e8454b62ba1 Mon Sep 17 00:00:00 2001 From: daovietanh99 Date: Tue, 30 Jan 2024 17:38:05 +0700 Subject: [PATCH] convert dict to xlsx --- cope2n-api/fwd_api/utils/file.py | 132 ++++++++++++++++++------------- cope2n-api/report_detail.xlsx | Bin 0 -> 6182 bytes 2 files changed, 77 insertions(+), 55 deletions(-) create mode 100644 cope2n-api/report_detail.xlsx diff --git a/cope2n-api/fwd_api/utils/file.py b/cope2n-api/fwd_api/utils/file.py index 266a038..17d22ee 100644 --- a/cope2n-api/fwd_api/utils/file.py +++ b/cope2n-api/fwd_api/utils/file.py @@ -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 diff --git a/cope2n-api/report_detail.xlsx b/cope2n-api/report_detail.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..3df13d8d91d576538b183207c12c63de1f61f725 GIT binary patch literal 6182 zcmaJ_1z3}7`v+lC0;54elx{|McM79Hgh`HOV<6qqjdY5LfYKpHaDbqsbdHjckQ5{Z z{_LD{ee(JJ&$-|0diFlMc0cdl-`zhQ4J>SO3|w4Xj0$~eV~lG;aCL71vvGp+@}So@ z$ph+Ge87ku|L8)Mvc!E9Ckpp3o?h)5=g2I21;ifdY0jFX@iu-`ADkTpwc z`y)C>C0g0F%#<@W{wK9>Q9jYpw>vG4M*WPR4b_>5t$NBvJDd*gCA(nO+yPBK)NMC@ zB)(`_OxqM;J!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