convert dict to xlsx

This commit is contained in:
daovietanh99 2024-01-30 16:33:42 +07:00
parent e4c439c7cd
commit cd88f3b959
2 changed files with 103 additions and 17 deletions

View File

@ -19,6 +19,9 @@ from ..celery_worker.client_connector import c_connector
import imagesize import imagesize
import csv import csv
from openpyxl import load_workbook
from openpyxl.styles import Font, Border, Side, PatternFill, NamedStyle
def validate_feedback_file(csv_file_path): def validate_feedback_file(csv_file_path):
required_columns = ['redemptionNumber', 'requestId', 'imeiNumber', 'imeiNumber2', 'Purchase Date', 'retailer', 'Sold to party', 'timetakenmilli'] required_columns = ['redemptionNumber', 'requestId', 'imeiNumber', 'imeiNumber2', 'Purchase Date', 'retailer', 'Sold to party', 'timetakenmilli']
missing_columns = [] missing_columns = []
@ -327,21 +330,104 @@ def build_media_url_v2(media_id: str, user_id: int, sub_id: int, u_sync_id: str)
token = image_authenticator.generate_img_token_v2(user_id, sub_id, u_sync_id) token = image_authenticator.generate_img_token_v2(user_id, sub_id, u_sync_id)
return f'{settings.BASE_URL}/api/ctel/v2/media/request/{media_id}/?token={token}' return f'{settings.BASE_URL}/api/ctel/v2/media/request/{media_id}/?token={token}'
def json2xlsx(input: json):
"""_summary_
Args: def get_value(_dict, keys):
input (json): keys = keys.split('.')
: [{ value = _dict
Subs: Jan, # Subtotal name for key in keys:
Metadata: {num_imei: 1, if not key in value.keys():
...: ...} return "-"
Data: [{num_imei: 1, else:
...: ...}] value = value.get(key, {})
}]
OR if value != 0:
input (json): return value
: [] else:
Return xlsx (object) return "-"
"""
pass
def dict2xlsx(input: json):
red = "FF0000"
black = "000000"
green = "E2EFDA"
yellow = "FFF2CC"
gray = "D0CECE"
font_black = Font(name="Calibri", size=11, color=black)
font_black_bold = Font(name="Calibri", size=11, color=black, bold=True)
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)
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")
normal_cell = NamedStyle(name="normal_cell", font=font_black, border=border)
normal_cell_red = NamedStyle(name="normal_cell_red", font=font_red, border=border)
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
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
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
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
print(type(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

BIN
cope2n-api/report.xlsx Normal file

Binary file not shown.