peux-tu me décrire ce que fait ce code : import xlsxwriter
import unicodedata
class Export:
header_format = {
'bg_color': '#32A852',
'font_color': '#FFFFFF',
'font_name': 'Roboto Condensed',
'font_size': 14,
'bold': True,
'align': 'center',
'valign': 'vcenter',
'shrink': True
}
question_format = {
'bg_color': '#54A86A',
'font_color': '#FFFFFF',
'font_name': 'Roboto Condensed',
'font_size': 14,
'bold': True,
'align': 'center',
'valign': 'vcenter',
}
total_format = {
'bg_color': '#54A86A',
'font_color': '#FFFFFF',
'font_name': 'Roboto Condensed',
'font_size': 14,
'bold': True,
'align': 'right',
'valign': 'vcenter',
'indent': 1,
'num_format': '0.00'
}
bold_format = {
'font_color': '#000000',
'font_name': 'Roboto Condensed',
'font_size': 12,
'bold': True,
'align': 'right',
'valign': 'vcenter',
'indent': 1,
'num_format': '0.00'
}
mark_format = {
'bg_color': '#93F5AD',
'font_color': '#000000',
'font_name': 'Roboto Condensed',
'font_size': 12,
'bold': True,
'align': 'right',
'valign': 'vcenter',
'indent': 1,
'num_format': '0.00'
}
error_format = {
'bg_color': '#DE9E9E',
'font_color': '#000000',
'font_name': 'Roboto Condensed',
'font_size': 12,
'bold': True,
'align': 'right',
'valign': 'vcenter',
'indent': 1,
'num_format': '0.00'
}
align_left_format = {
'font_color': '#000000',
'font_name': 'Roboto Condensed',
'font_size': 12,
'align': 'left',
'valign': 'vcenter',
'indent': 1
}
align_center_format = {
'font_color': '#000000',
'font_name': 'Roboto Condensed',
'font_size': 12,
'align': 'center',
'valign': 'vcenter'
}
align_right_format = {
'font_color': '#000000',
'font_name': 'Roboto Condensed',
'font_size': 12,
'align': 'right',
'valign': 'vcenter',
'indent': 1,
'num_format': '0.00'
}
def export_qcm(self, info, evaluation, id_evaluation):
path = f"main/qcm/export/{id_evaluation}.xlsx"
output = xlsxwriter.Workbook(path)
var permettant l'indexion des colones de totaux
same_total = self.same_total_all_qcm(evaluation)
col_total_20 = "E1" if same_total else self.col_total_20(len(evaluation.qcms))
index_col_total_20 = 4 if same_total else 3 + (len(evaluation.qcms) -1)
index_first_col_total = 'D'
--- DÉFINITION DES DIFFÉRENTS FORMATS ---
header_format = output.add_format(self.header_format)
question_format = output.add_format(self.question_format)
total_format = output.add_format(self.total_format)
mark_format = output.add_format(self.mark_format)
bold_format = output.add_format(self.bold_format)
error_format = output.add_format(self.error_format)
align_left_format = output.add_format(self.align_left_format)
align_center_format = output.add_format(self.align_center_format)
align_right_format = output.add_format(self.align_right_format)
--- FEUILLE TOTAL ---
sheet_total = output.add_worksheet('TOTAL')
sheet_total.set_column_pixels('A:A', 90) # 81 Minimum
sheet_total.set_column_pixels('B:B', 55) # 45 Minimum
sheet_total.set_column_pixels('C:C', 220)
sheet_total.set_column_pixels('D:H', 70)
sheet_total.freeze_panes(1, 0)
sheet_total.write("A1", "Matricule", header_format)
sheet_total.write("B1", "QCM", header_format)
sheet_total.write("C1", "Nom / Prénom", header_format)
#insert une colonne total pour chaque questionnaire si le total des questionnaires est différent
if same_total:
sheet_total.write("D1", "Total", header_format)
else:
for i in range(len(evaluation.qcms)-1):
sheet_total.write(index_first_col_total + "1", "Total", header_format)
index_first_col_total = chr(ord(index_first_col_total) + 1)
sheet_total.write(col_total_20, "Total / 20", header_format)
sheet_total.conditional_format(
'A1:B1500',
{
'type': 'cell',
'criteria': '==',
'value': '"x"',
'format': error_format
}
)
sheet_total.conditional_format(
'A1:A1500',
{
'type': 'duplicate',
'format': error_format
}
)
fin_analyse = len(evaluation.qcms) + len(info) - 1
sheet_total.conditional_format(
f'D1:{col_total_20[0]}{fin_analyse}',
{
'type': 'cell',
'criteria': '<',
'value': 0,
'format': error_format
}
)
Afficher les cases Q1, Q2, ...
number_questionnaire = 0
k = 0
students_data = []
#char_to_replace = {'é': 'e', 'è': 'e', 'à': 'a'}
for questionnaire in evaluation.qcms:
maximum = 0
for number_question in range(len(questionnaire.questions)):
maximum += questionnaire.questions[number_question].quotation.pointsOk
for number_question in range(len(info)):
if info[number_question]["scan_type"].lower() == questionnaire.type.lower():
students_data.append({"info": info[number_question], "max": maximum})
col = 3 if same_total else self.index_col_total_qcm(questionnaire.type.lower())
type_or_empty = "" if same_total else questionnaire.type
max_to_write = str(round(maximum))
sheet_total.write(0, col, "Total " + type_or_empty + " / " + max_to_write, header_format)
students_data_sorted = sorted(students_data, key=lambda a: self.remove_accents(a["info"]['nom'].replace(" ", "")
.replace("'", "").lower()))
for data in students_data_sorted:
question = data["info"]
maximum = data["max"]
col = 3 if same_total else self.index_col_total_qcm(question["scan_type"])
type_or_empty = "" if same_total else question["scan_type"]
sheet_total.write(k + 1, 0, question["matricule"].upper(),
align_left_format)
sheet_total.write(k + 1, 1, question["scan_type"],
align_center_format)
sheet_total.write(k + 1, 2,
f"{question['nom'].upper()} {question['prenom']}", align_left_format)
if maximum != 0:
sheet_total.write(k + 1, index_col_total_20, question["total"]/maximum * 20, bold_format)
sheet_total.write(k + 1, col, question["total"], bold_format)
number_to_write = 0
if maximum % 1 == 0:
number_to_write = str(round(maximum))
else:
number_to_write=str(maximum)
sheet_total.write(0, col, "Total " + type_or_empty + " / " + number_to_write, header_format)
k += 1
number_questionnaire += 1
--- FEUILLE DÉTAILS ---
sheet_details = output.add_worksheet('DÉTAILS')
sheet_details.set_column_pixels('A:A', 90) # 81 Minimum
sheet_details.set_column_pixels('B:B', 55) # 45 Minimum
sheet_details.set_column_pixels('C:C', 220)
sheet_details.set_column_pixels('D:D', 70)
sheet_details.freeze_panes(1, 0)
sheet_details.write("A1", "Matricule", header_format)
sheet_details.write("B1", "QCM", header_format)
sheet_details.write("C1", "Nom / Prénom", header_format)
sheet_details.write("D1", "Total", header_format)
sheet_details.conditional_format(
'A1:B1500',
{
'type': 'cell',
'criteria': '==',
'value': '"x"',
'format': error_format
}
)
sheet_details.conditional_format(
'A1:A1500',
{
'type': 'duplicate',
'format': error_format
}
)
sheet_details.conditional_format(
f'D1:D{fin_analyse}',
{
'type': 'cell',
'criteria': '<',
'value': 0,
'format': error_format
}
)
fin_analyse = len(evaluation.qcms) + len(info) - 1
Afficher les cases Q1, Q2, ...
for number_question in range(int(info[0]["nbrQ"])):
sheet_details.write(0, 4 + number_question, f"Q{number_question + 1}", question_format)
number_questionnaire = 0
k = 0
for questionnaire in evaluation.qcms:
maximum = 0
sheet_details.merge_range(1 + number_questionnaire + k, 0,
1 + number_questionnaire + k, 2,
f"QUESTIONNAIRE {questionnaire.type.upper()}", question_format)
for number_question in range(len(questionnaire.questions)):
sheet_details.write(
1 + number_questionnaire + k,
4 + number_question,
questionnaire.questions[number_question].quotation.pointsOk,
mark_format
)
maximum += questionnaire.questions[number_question].quotation.pointsOk
sheet_details.write(1 + number_questionnaire + k, 3, maximum, total_format)
questions_from_this_questionnaire = []
for number_question in range(len(info)):
if info[number_question]["scan_type"].lower() == questionnaire.type.lower():
questions_from_this_questionnaire.append(info[number_question])
questions_from_this_questionnaire = sorted(questions_from_this_questionnaire, key=lambda a:
self.remove_accents(a['nom'].replace(" ", "")
.replace("'", "").lower()))
for question in questions_from_this_questionnaire:
sheet_details.write(k + 2 + number_questionnaire, 0, question["matricule"].upper(),
align_left_format)
sheet_details.write(k + 2 + number_questionnaire, 1, question["scan_type"],
align_center_format)
sheet_details.write(k + 2 + number_questionnaire, 2,
f"{question['nom'].upper()} {question['prenom']}",
align_left_format)
sheet_details.write(k + 2 + number_questionnaire, 3, question["total"], bold_format)
for j in range(len(question["notes"])):
sheet_details.write(2 + number_questionnaire + k, 4 + j,
question["notes"][j]["pointObtenu"], align_right_format)
k += 1
number_questionnaire += 1
output.close()
return "qcm/export/" + id_evaluation + ".xlsx"
retourne un boolean qui indique si les questionnaires ont le même total ou non
@staticmethod
def same_total_all_qcm(evaluation):
maximum = []
for questionnaire in evaluation.qcms:
if questionnaire.type == 'x':
break
max_qcm = 0
for number_question in range(len(questionnaire.questions)):
max_qcm += questionnaire.questions[number_question].quotation.pointsOk
maximum.append(max_qcm)
return all(total == maximum[0] for total in maximum)
retourne l'index de la colonne du total sur 20
@staticmethod
def col_total_20(nb_qcm):
ch = chr(ord('D') + (nb_qcm-1))
return ch + "1"
retourn l'index de la colonne selon le type de qcm (A,B...)
@staticmethod
def index_col_total_qcm(type_qcm):
pos = ord(type_qcm.lower()) - 97
return pos + 3
def remove_accents(self, input_str):
nfkd_form = unicodedata.normalize('NFKD', input_str)
only_ascii = nfkd_form.encode('ASCII', 'ignore')
return only_ascii
#fonction qui enlève les accents et les espaces
def remove_accents_spaces_and_apostrophe(self, input_str):
input_str = input_str.replace(" ", "").replace("'","").lower()
nfkd_form = unicodedata.normalize('NFKD', input_str)
only_ascii = nfkd_form.encode('ASCII', 'ignore')
return only_ascii.replace(" ", "").replace("'","")