update number format

This commit is contained in:
PhanThanhTrung 2024-03-21 10:24:20 +07:00
parent 5343623b59
commit 4b36448ede

View File

@ -461,6 +461,15 @@ def get_value(_dict, keys):
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"
black = "000000"
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_yellow = PatternFill(start_color=yellow, end_color=yellow, fill_type = "solid")
fill_gray = PatternFill(start_color=gray, end_color=gray, fill_type = "solid")
align_right = Alignment(horizontal='right')
if _type == 'report':
wb = load_workbook(filename = 'report.xlsx')
ws = wb['Sheet1']
mapping = {
'A': 'subs',
'B': 'extraction_date',
'C': 'usage.total_images',
'D': 'usage.imei',
'E': 'usage.invoice',
'F': 'total_images',
'G': 'num_imei',
'H': 'num_invoice',
'I': 'images_quality.successful',
'J': 'images_quality.successful_percent',
'K': 'images_quality.bad',
'L': 'images_quality.bad_percent',
'M': 'average_accuracy_rate.imei',
'N': 'average_accuracy_rate.purchase_date',
'O': 'average_accuracy_rate.retailer_name',
'P': 'average_accuracy_rate.invoice_number',
'Q': 'average_processing_time.avg',
'R': 'review_progress'
}
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
wb = load_workbook(filename = 'report.xlsx')
ws = wb['Sheet1']
mapping = {
'A': 'subs',
'B': 'extraction_date',
'C': 'usage.total_images',
'D': 'usage.imei',
'E': 'usage.invoice',
'F': 'total_images',
'G': 'num_imei',
'H': 'num_invoice',
'I': 'images_quality.successful',
'J': 'images_quality.successful_percent',
'K': 'images_quality.bad',
'L': 'images_quality.bad_percent',
'M': 'average_accuracy_rate.imei',
'N': 'average_accuracy_rate.purchase_date',
'O': 'average_accuracy_rate.retailer_name',
'P': 'average_accuracy_rate.invoice_number',
'Q': 'average_processing_time.avg',
'R': 'review_progress'
}
start_index = 5
for subtotal in input:
for key in mapping.keys():
if _type!="billing_report":
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'
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 _type == 'report':
if subtotal['subs'] == '+':
ws[key + str(start_index)].font = font_black_bold
if key in ['A', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R']:
ws[key + str(start_index)].fill = fill_gray
elif key == 'B':
ws[key + str(start_index)].fill = fill_green
elif key in ['C', 'D', 'E', 'F', 'G', 'H']:
ws[key + str(start_index)].fill = fill_yellow
if 'average_accuracy_rate' in mapping[key] and type(value) in [int, float] and value < 98:
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:
ws[key + str(start_index)].font = font_red
elif 'bad_percent' in mapping[key] and type(value) in [int, float] and value > 10:
ws[key + str(start_index)].font = font_red
elif _type == 'report_detail':
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
else:
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
if subtotal['subs'] == '+':
ws[key + str(start_index)].font = font_black_bold
if key in ['A', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R']:
ws[key + str(start_index)].fill = fill_gray
elif key == 'B':
ws[key + str(start_index)].fill = fill_green
elif key in ['C', 'D', 'E', 'F', 'G', 'H']:
ws[key + str(start_index)].fill = fill_yellow
if 'average_accuracy_rate' in mapping[key] and type(value) in [int, float] and value < 98:
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:
ws[key + str(start_index)].font = font_red
elif 'bad_percent' in mapping[key] and type(value) in [int, float] and value > 10:
ws[key + str(start_index)].font = font_red
if key in ['C', 'D', 'E', 'F', 'G', 'H', 'I', 'K']:
ws[key + str(start_index)].number_format= '#,##0'
start_index += 1
return wb
def dump_excel_report_detail(input: json):
red = "FF0000"
black = "000000"
font_black = Font(name="Calibri", size=11, color=black)
font_red = Font(name="Calibri", size=11, color=red)
thin = Side(border_style="thin", color=black)
border = Border(left=thin, right=thin, top=thin, bottom=thin)
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
return wb
return wb