Merge pull request #97 from SDSRV-IDP/fix_excel_number_format

update number format
This commit is contained in:
Đỗ Xuân Tân 2024-03-21 10:25:34 +07:00 committed by GitHub Enterprise
commit dc1a9097de

View File

@ -461,6 +461,15 @@ def get_value(_dict, keys):
def dict2xlsx(input: json, _type='report'): def dict2xlsx(input: json, _type='report'):
if _type == 'report':
wb = dump_excel_report(input=input)
elif _type == 'report_detail':
wb = dump_excel_report_detail(input=input)
elif _type == 'billing_report':
wb = dump_excel_billing_report(input=input)
return wb
def dump_excel_report(input: json):
red = "FF0000" red = "FF0000"
black = "000000" black = "000000"
green = "E2EFDA" green = "E2EFDA"
@ -474,123 +483,148 @@ def dict2xlsx(input: json, _type='report'):
fill_green = PatternFill(start_color=green, end_color=green, fill_type = "solid") fill_green = PatternFill(start_color=green, end_color=green, fill_type = "solid")
fill_yellow = PatternFill(start_color=yellow, end_color=yellow, fill_type = "solid") fill_yellow = PatternFill(start_color=yellow, end_color=yellow, fill_type = "solid")
fill_gray = PatternFill(start_color=gray, end_color=gray, fill_type = "solid") fill_gray = PatternFill(start_color=gray, end_color=gray, fill_type = "solid")
align_right = Alignment(horizontal='right') wb = load_workbook(filename = 'report.xlsx')
if _type == 'report': ws = wb['Sheet1']
wb = load_workbook(filename = 'report.xlsx') mapping = {
ws = wb['Sheet1'] 'A': 'subs',
mapping = { 'B': 'extraction_date',
'A': 'subs', 'C': 'usage.total_images',
'B': 'extraction_date', 'D': 'usage.imei',
'C': 'usage.total_images', 'E': 'usage.invoice',
'D': 'usage.imei', 'F': 'total_images',
'E': 'usage.invoice', 'G': 'num_imei',
'F': 'total_images', 'H': 'num_invoice',
'G': 'num_imei', 'I': 'images_quality.successful',
'H': 'num_invoice', 'J': 'images_quality.successful_percent',
'I': 'images_quality.successful', 'K': 'images_quality.bad',
'J': 'images_quality.successful_percent', 'L': 'images_quality.bad_percent',
'K': 'images_quality.bad', 'M': 'average_accuracy_rate.imei',
'L': 'images_quality.bad_percent', 'N': 'average_accuracy_rate.purchase_date',
'M': 'average_accuracy_rate.imei', 'O': 'average_accuracy_rate.retailer_name',
'N': 'average_accuracy_rate.purchase_date', 'P': 'average_accuracy_rate.invoice_number',
'O': 'average_accuracy_rate.retailer_name', 'Q': 'average_processing_time.avg',
'P': 'average_accuracy_rate.invoice_number', 'R': 'review_progress'
'Q': 'average_processing_time.avg', }
'R': 'review_progress' start_index = 5
}
start_index = 5
elif _type == 'report_detail':
wb = load_workbook(filename = 'report_detail.xlsx')
ws = wb['Sheet1']
mapping = {
'A': 'subs',
'B': 'request_id',
'C': "ocr_extraction_date",
'D': 'redemption_number',
'E': 'image_type',
'F': 'imei_user_submitted',
'G': "imei_ocr_retrieved",
'H': "imei_revised",
'I': "imei1_accuracy",
'J': "invoice_number_user",
'K': "invoice_number_ocr",
'L': "invoice_number_revised",
'M': "invoice_number_accuracy",
'N': "invoice_purchase_date_consumer",
'O': "invoice_purchase_date_ocr",
'P': "invoice_purchase_date_revised",
'Q': "invoice_purchase_date_accuracy",
'R': "invoice_retailer_consumer",
'S': "invoice_retailer_ocr",
'T': 'invoice_retailer_revised',
'U': "invoice_retailer_accuracy",
'V': "ocr_image_accuracy",
'W': "ocr_image_speed_(seconds)",
'X': "is_reviewed",
'Y': "bad_image_reasons",
'Z': "countermeasures",
'AA':"imei_revised_accuracy",
'AB': "invoice_number_revised_accuracy",
'AC': 'purchase_date_revised_accuracy',
'AD':'retailer_revised_accuracy',
}
start_index = 4
elif _type == 'billing_report':
wb = load_workbook(filename = 'billing_report.xlsx')
ws = wb['Sheet1']
mapping = {
'B': 'request_month',
'C': 'subsidiary',
'D': 'image_type',
'E': 'redemption_number',
'F': 'request_id',
'G': "request_date",
'H': "request_time_(utc)"
}
start_index = 4
for subtotal in input: for subtotal in input:
for key in mapping.keys(): for key in mapping.keys():
if _type!="billing_report": value = get_value(subtotal, mapping[key])
value = get_value(subtotal, mapping[key]) ws[key + str(start_index)] = value
ws[key + str(start_index)] = value if key in ['C', 'D', 'E'] and value == 0:
if key in ['C', 'D', 'E'] and value == 0: ws[key + str(start_index)] = "-"
ws[key + str(start_index)] = "-" ws[key + str(start_index)].border = border
ws[key + str(start_index)].border = border ws[key + str(start_index)].font = font_black
ws[key + str(start_index)].font = font_black if 'accuracy' in mapping[key] or 'time' in mapping[key] or 'percent' in mapping[key] or 'speed' in mapping[key] or mapping[key] in ["review_progress"]:
if 'accuracy' in mapping[key] or 'time' in mapping[key] or 'percent' in mapping[key] or 'speed' in mapping[key] or mapping[key] in ["review_progress"]: ws[key + str(start_index)].number_format = '0.0'
ws[key + str(start_index)].number_format = '0.0'
if _type == 'report': if subtotal['subs'] == '+':
if subtotal['subs'] == '+': ws[key + str(start_index)].font = font_black_bold
ws[key + str(start_index)].font = font_black_bold if key in ['A', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R']:
if key in ['A', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R']: ws[key + str(start_index)].fill = fill_gray
ws[key + str(start_index)].fill = fill_gray elif key == 'B':
elif key == 'B': ws[key + str(start_index)].fill = fill_green
ws[key + str(start_index)].fill = fill_green elif key in ['C', 'D', 'E', 'F', 'G', 'H']:
elif key in ['C', 'D', 'E', 'F', 'G', 'H']: ws[key + str(start_index)].fill = fill_yellow
ws[key + str(start_index)].fill = fill_yellow if 'average_accuracy_rate' in mapping[key] and type(value) in [int, float] and value < 98:
if 'average_accuracy_rate' in mapping[key] and type(value) in [int, float] and value < 98: ws[key + str(start_index)].font = font_red
ws[key + str(start_index)].font = font_red elif 'average_processing_time' in mapping[key] and type(value) in [int, float] and value > 2.0:
elif 'average_processing_time' in mapping[key] and type(value) in [int, float] and value > 2.0: ws[key + str(start_index)].font = font_red
ws[key + str(start_index)].font = font_red elif 'bad_percent' in mapping[key] and type(value) in [int, float] and value > 10:
elif 'bad_percent' in mapping[key] and type(value) in [int, float] and value > 10: ws[key + str(start_index)].font = font_red
ws[key + str(start_index)].font = font_red
elif _type == 'report_detail': if key in ['C', 'D', 'E', 'F', 'G', 'H', 'I', 'K']:
if 'accuracy' in mapping[key] and type(value) in [int, float] and value < 75: ws[key + str(start_index)].number_format= '#,##0'
ws[key + str(start_index)].font = font_red start_index += 1
elif 'speed' in mapping[key] and type(value) in [int, float] and value > 2.0: return wb
ws[key + str(start_index)].font = font_red
else: def dump_excel_report_detail(input: json):
value = get_value(subtotal, mapping[key]) red = "FF0000"
value = "-" if value=="" else value black = "000000"
ws[key + str(start_index)] = value font_black = Font(name="Calibri", size=11, color=black)
ws[key + str(start_index)].border = border font_red = Font(name="Calibri", size=11, color=red)
ws[key + str(start_index)].font = font_black thin = Side(border_style="thin", color=black)
if key in ['G', 'H']: border = Border(left=thin, right=thin, top=thin, bottom=thin)
ws[key + str(start_index)].alignment = align_right wb = load_workbook(filename = 'report_detail.xlsx')
ws = wb['Sheet1']
mapping = {
'A': 'subs',
'B': 'request_id',
'C': "ocr_extraction_date",
'D': 'redemption_number',
'E': 'image_type',
'F': 'imei_user_submitted',
'G': "imei_ocr_retrieved",
'H': "imei_revised",
'I': "imei1_accuracy",
'J': "invoice_number_user",
'K': "invoice_number_ocr",
'L': "invoice_number_revised",
'M': "invoice_number_accuracy",
'N': "invoice_purchase_date_consumer",
'O': "invoice_purchase_date_ocr",
'P': "invoice_purchase_date_revised",
'Q': "invoice_purchase_date_accuracy",
'R': "invoice_retailer_consumer",
'S': "invoice_retailer_ocr",
'T': 'invoice_retailer_revised',
'U': "invoice_retailer_accuracy",
'V': "ocr_image_accuracy",
'W': "ocr_image_speed_(seconds)",
'X': "is_reviewed",
'Y': "bad_image_reasons",
'Z': "countermeasures",
'AA':"imei_revised_accuracy",
'AB': "invoice_number_revised_accuracy",
'AC': 'purchase_date_revised_accuracy',
'AD':'retailer_revised_accuracy',
}
start_index = 4
for subtotal in input:
for key in mapping.keys():
value = get_value(subtotal, mapping[key])
ws[key + str(start_index)] = value
if key in ['C', 'D', 'E'] and value == 0:
ws[key + str(start_index)] = "-"
ws[key + str(start_index)].border = border
ws[key + str(start_index)].font = font_black
if 'accuracy' in mapping[key] or 'time' in mapping[key] or 'percent' in mapping[key] or 'speed' in mapping[key] or mapping[key] in ["review_progress"]:
ws[key + str(start_index)].number_format = '0.0'
if 'accuracy' in mapping[key] and type(value) in [int, float] and value < 75:
ws[key + str(start_index)].font = font_red
elif 'speed' in mapping[key] and type(value) in [int, float] and value > 2.0:
ws[key + str(start_index)].font = font_red
start_index += 1
return wb
def dump_excel_billing_report(input: json):
black = "000000"
font_black = Font(name="Calibri", size=11, color=black)
thin = Side(border_style="thin", color=black)
border = Border(left=thin, right=thin, top=thin, bottom=thin)
align_right = Alignment(horizontal='right')
wb = load_workbook(filename = 'billing_report.xlsx')
ws = wb['Sheet1']
mapping = {
'B': 'request_month',
'C': 'subsidiary',
'D': 'image_type',
'E': 'redemption_number',
'F': 'request_id',
'G': "request_date",
'H': "request_time_(utc)"
}
start_index = 4
for subtotal in input:
for key in mapping.keys():
value = get_value(subtotal, mapping[key])
value = "-" if value=="" else value
ws[key + str(start_index)] = value
ws[key + str(start_index)].border = border
ws[key + str(start_index)].font = font_black
if key in ['G', 'H']:
ws[key + str(start_index)].alignment = align_right
start_index += 1 start_index += 1
return wb return wb