|
from flask import Flask, render_template, request, send_file |
|
import os |
|
import convertapi |
|
from docx import Document |
|
from docx.shared import Pt, Cm, RGBColor |
|
from docx.enum.text import WD_ALIGN_PARAGRAPH |
|
from docx.enum.table import WD_TABLE_ALIGNMENT |
|
from docx.oxml.ns import nsdecls |
|
from docx.oxml import parse_xml |
|
import math |
|
|
|
|
|
convertapi.api_credentials = 'secret_8wCI6pgOP9AxLVJG' |
|
|
|
|
|
class EvaluationGymnique: |
|
def __init__(self): |
|
self.document = Document() |
|
self.document.sections[0].page_height = Cm(29.7) |
|
self.document.sections[0].page_width = Cm(21) |
|
self.document.sections[0].left_margin = Cm(1.5) |
|
self.document.sections[0].right_margin = Cm(1.5) |
|
self.document.sections[0].top_margin = Cm(1) |
|
self.document.sections[0].bottom_margin = Cm(1) |
|
|
|
|
|
self.centre_examen = "Centre d'examen" |
|
self.type_examen = "Bac Général" |
|
self.serie = "Série" |
|
self.etablissement = "Établissement" |
|
self.session = "2025" |
|
self.nom_candidat = "Candidat" |
|
|
|
|
|
self.elements_techniques = [] |
|
self.appreciations = ["M", "PM", "NM", "NR"] |
|
|
|
|
|
self.base_font_size = 10 |
|
self.base_header_font_size = 14 |
|
self.base_row_height = 1 |
|
self.table_font_size = 9 |
|
|
|
|
|
self.available_height = 27.7 |
|
|
|
|
|
self.fixed_elements_height = 14 |
|
|
|
def calculate_dynamic_sizing(self): |
|
"""Calcule les tailles dynamiques en fonction du nombre d'éléments techniques""" |
|
|
|
num_elements = len(self.elements_techniques) |
|
|
|
|
|
|
|
required_table_height = (num_elements + 1) * self.base_row_height |
|
|
|
|
|
available_space = self.available_height - self.fixed_elements_height |
|
|
|
|
|
if required_table_height > available_space and num_elements > 0: |
|
adjustment_factor = available_space / required_table_height |
|
|
|
if num_elements <= 5: |
|
|
|
self.dynamic_font_size = max(self.base_font_size - 1, 7) |
|
self.dynamic_header_font_size = max(self.base_header_font_size - 1, 12) |
|
self.dynamic_table_font_size = max(self.table_font_size - 1, 7) |
|
self.dynamic_row_height = self.base_row_height * 0.9 |
|
self.spacing_factor = 0.9 |
|
elif num_elements <= 10: |
|
|
|
self.dynamic_font_size = max(self.base_font_size - 2, 6.5) |
|
self.dynamic_header_font_size = max(self.base_header_font_size - 2, 11) |
|
self.dynamic_table_font_size = max(self.table_font_size - 2, 6.5) |
|
self.dynamic_row_height = self.base_row_height * 0.75 |
|
self.spacing_factor = 0.7 |
|
elif num_elements <= 15: |
|
|
|
self.dynamic_font_size = max(self.base_font_size - 3, 6) |
|
self.dynamic_header_font_size = max(self.base_header_font_size - 3, 10) |
|
self.dynamic_table_font_size = max(self.table_font_size - 3, 6) |
|
self.dynamic_row_height = self.base_row_height * 0.6 |
|
self.spacing_factor = 0.5 |
|
else: |
|
|
|
self.dynamic_font_size = max(self.base_font_size - 4, 5.5) |
|
self.dynamic_header_font_size = max(self.base_header_font_size - 4, 9) |
|
self.dynamic_table_font_size = max(self.table_font_size - 4, 5.5) |
|
self.dynamic_row_height = max(self.base_row_height * 0.5, 0.4) |
|
self.spacing_factor = 0.3 |
|
else: |
|
|
|
self.dynamic_font_size = self.base_font_size |
|
self.dynamic_header_font_size = self.base_header_font_size |
|
self.dynamic_table_font_size = self.table_font_size |
|
self.dynamic_row_height = self.base_row_height |
|
self.spacing_factor = 1.0 |
|
|
|
def ajouter_entete_colore(self): |
|
header_paragraph = self.document.add_paragraph() |
|
header_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
header_paragraph.space_after = Pt(6 * self.spacing_factor) |
|
header_run = header_paragraph.add_run("ÉVALUATION GYMNASTIQUE") |
|
header_run.bold = True |
|
header_run.font.size = Pt(self.dynamic_header_font_size) |
|
header_run.font.color.rgb = RGBColor(0, 32, 96) |
|
|
|
header_table = self.document.add_table(rows=3, cols=2) |
|
header_table.style = 'Table Grid' |
|
header_table.autofit = False |
|
|
|
|
|
row_height = Cm(0.8 * (self.dynamic_row_height / self.base_row_height)) |
|
for row in header_table.rows: |
|
row.height = row_height |
|
|
|
for row in header_table.rows: |
|
for cell in row.cells: |
|
shading_elm = parse_xml(f'<w:shd {nsdecls("w")} w:fill="D9E2F3"/>') |
|
cell._tc.get_or_add_tcPr().append(shading_elm) |
|
|
|
|
|
cell = header_table.cell(0, 0) |
|
paragraph = cell.paragraphs[0] |
|
run = paragraph.add_run("Centre d'examen: ") |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_font_size) |
|
run.font.color.rgb = RGBColor(0, 32, 96) |
|
paragraph.add_run(self.centre_examen).font.size = Pt(self.dynamic_font_size) |
|
|
|
cell = header_table.cell(0, 1) |
|
paragraph = cell.paragraphs[0] |
|
run = paragraph.add_run("Examen: ") |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_font_size) |
|
run.font.color.rgb = RGBColor(0, 32, 96) |
|
paragraph.add_run(self.type_examen).font.size = Pt(self.dynamic_font_size) |
|
|
|
|
|
cell = header_table.cell(1, 0) |
|
paragraph = cell.paragraphs[0] |
|
run = paragraph.add_run("Série: ") |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_font_size) |
|
run.font.color.rgb = RGBColor(0, 32, 96) |
|
paragraph.add_run(self.serie).font.size = Pt(self.dynamic_font_size) |
|
|
|
cell = header_table.cell(1, 1) |
|
paragraph = cell.paragraphs[0] |
|
run = paragraph.add_run("Établissement: ") |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_font_size) |
|
run.font.color.rgb = RGBColor(0, 32, 96) |
|
paragraph.add_run(self.etablissement).font.size = Pt(self.dynamic_font_size) |
|
|
|
|
|
cell = header_table.cell(2, 0) |
|
paragraph = cell.paragraphs[0] |
|
run = paragraph.add_run("Session: ") |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_font_size) |
|
run.font.color.rgb = RGBColor(0, 32, 96) |
|
paragraph.add_run(self.session).font.size = Pt(self.dynamic_font_size) |
|
|
|
cell = header_table.cell(2, 1) |
|
paragraph = cell.paragraphs[0] |
|
run = paragraph.add_run("Candidat: ") |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_font_size) |
|
run.font.color.rgb = RGBColor(0, 32, 96) |
|
paragraph.add_run(self.nom_candidat).font.size = Pt(self.dynamic_font_size) |
|
|
|
|
|
self.document.add_paragraph().space_after = Pt(4 * self.spacing_factor) |
|
|
|
def creer_tableau_elements(self): |
|
table = self.document.add_table(rows=len(self.elements_techniques) + 1, cols=5) |
|
table.style = 'Table Grid' |
|
table.alignment = WD_TABLE_ALIGNMENT.CENTER |
|
|
|
|
|
total_width = 18 |
|
|
|
|
|
col_widths = [8, 3, 2, 2.5, 2.5] |
|
for i, width in enumerate(col_widths): |
|
for cell in table.columns[i].cells: |
|
cell.width = Cm(width) |
|
|
|
|
|
for row in table.rows: |
|
row.height = Cm(self.dynamic_row_height) |
|
|
|
|
|
header_row = table.rows[0] |
|
for cell in header_row.cells: |
|
shading_elm = parse_xml(f'<w:shd {nsdecls("w")} w:fill="BDD7EE"/>') |
|
cell._tc.get_or_add_tcPr().append(shading_elm) |
|
|
|
headers = ["ELEMENTS TECHNIQUES", "CATEGORIES D'ELEMENTS TECHNIQUES ET PONDERATION", "", "APPRECIATIONS", "POINTS Accordés"] |
|
for i, header in enumerate(headers): |
|
cell = table.cell(0, i) |
|
cell.text = header |
|
cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
run = cell.paragraphs[0].runs[0] |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_table_font_size) |
|
run.font.color.rgb = RGBColor(0, 32, 96) |
|
table.cell(0, 1).merge(table.cell(0, 2)) |
|
|
|
|
|
for i, element in enumerate(self.elements_techniques, 1): |
|
element_cell = table.cell(i, 0) |
|
element_cell.text = element["nom"] |
|
element_cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.LEFT |
|
run = element_cell.paragraphs[0].runs[0] |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_table_font_size) |
|
|
|
categorie_cell = table.cell(i, 1) |
|
categorie_cell.text = element["categorie"] |
|
categorie_cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
run = categorie_cell.paragraphs[0].runs[0] |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_table_font_size) |
|
run.italic = True |
|
|
|
points_cell = table.cell(i, 2) |
|
points_cell.text = str(element["points"]) |
|
points_cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
run = points_cell.paragraphs[0].runs[0] |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_table_font_size) |
|
run.italic = True |
|
|
|
def ajouter_note_jury(self): |
|
para = self.document.add_paragraph() |
|
para.space_before = Pt(4 * self.spacing_factor) |
|
para.space_after = Pt(4 * self.spacing_factor) |
|
run = para.add_run("NB1 : Zone réservée aux membres du jury ! Le jury cochera le point correspondant au niveau de réalisation de l'élément gymnique par le candidat.") |
|
run.bold = True |
|
run.font.color.rgb = RGBColor(255, 0, 0) |
|
run.font.size = Pt(max(self.dynamic_font_size - 2, 5.5)) |
|
|
|
def creer_tableau_recapitulatif(self): |
|
note_table = self.document.add_table(rows=3, cols=13) |
|
note_table.style = 'Table Grid' |
|
note_table.alignment = WD_TABLE_ALIGNMENT.CENTER |
|
|
|
|
|
for row in note_table.rows: |
|
row.height = Cm(0.6 * (self.dynamic_row_height / self.base_row_height)) |
|
|
|
for cell in note_table.rows[0].cells: |
|
shading_elm = parse_xml(f'<w:shd {nsdecls("w")} w:fill="BDD7EE"/>') |
|
cell._tc.get_or_add_tcPr().append(shading_elm) |
|
|
|
|
|
recap_font_size = max(self.dynamic_table_font_size - 1, 5) |
|
|
|
for col, (type_lettre, points) in enumerate([("A", "1pt"), ("B", "1,5pt"), ("C", "2pts"), ("D", "2,5pts"), ("E", "3pts")]): |
|
idx = col * 2 |
|
if idx + 1 < len(note_table.columns): |
|
cell = note_table.cell(0, idx) |
|
cell.merge(note_table.cell(0, idx + 1)) |
|
cell.text = f"Type {type_lettre}\n{points}" |
|
cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
for run in cell.paragraphs[0].runs: |
|
run.bold = True |
|
run.font.size = Pt(recap_font_size) |
|
run.font.color.rgb = RGBColor(0, 32, 96) |
|
|
|
for col, (titre, points) in enumerate([("ROV", "2pts"), ("Projet", "2pts"), ("Réalisation", "16pts")], 10): |
|
if col < len(note_table.columns): |
|
cell = note_table.cell(0, col) |
|
cell.text = f"{titre}\n{points}" |
|
cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
for run in cell.paragraphs[0].runs: |
|
run.bold = True |
|
run.font.size = Pt(recap_font_size) |
|
run.font.color.rgb = RGBColor(0, 32, 96) |
|
|
|
for col in range(5): |
|
idx = col * 2 |
|
if idx + 1 < len(note_table.columns): |
|
neg_cell = note_table.cell(1, idx) |
|
neg_cell.text = "NEG" |
|
neg_cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
for run in neg_cell.paragraphs[0].runs: |
|
run.italic = True |
|
run.font.size = Pt(recap_font_size) |
|
|
|
note_cell = note_table.cell(1, idx + 1) |
|
note_cell.text = "Note" |
|
note_cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
for run in note_cell.paragraphs[0].runs: |
|
run.italic = True |
|
run.font.size = Pt(recap_font_size) |
|
|
|
for col in range(10, 13): |
|
if col < len(note_table.columns): |
|
cell = note_table.cell(1, col) |
|
cell.text = "Note" |
|
cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
for run in cell.paragraphs[0].runs: |
|
run.italic = True |
|
run.font.size = Pt(recap_font_size) |
|
|
|
def ajouter_note_candidat_avec_cadre(self): |
|
note_table = self.document.add_table(rows=1, cols=1) |
|
note_table.style = 'Table Grid' |
|
note_table.alignment = WD_TABLE_ALIGNMENT.CENTER |
|
|
|
cell = note_table.cell(0, 0) |
|
shading_elm = parse_xml(f'<w:shd {nsdecls("w")} w:fill="E2EFDA"/>') |
|
cell._tc.get_or_add_tcPr().append(shading_elm) |
|
|
|
p = cell.paragraphs[0] |
|
|
|
|
|
font_size = max(6 * self.spacing_factor, 5) |
|
|
|
run = p.add_run("NB2: Après le choix des catégories d'éléments gymniques par le candidat, ce dernier remplira la colonne de pointage selon l'orientation suivante: A (0.25; 0.5; 0.75; 1) B (0.25; 0.5; 0.75; 1; 1.25; 1.5) C (0.5; 0.75; 1; 1.25; 1.5; 2) D (0.75; 1; 1.25; 1.5; 2; 2.5) et E (0.75; 1; 1.5; 2; 2.5; 3) également, le candidat devra fournir 2 copies de son projet sur une page! (appréciations: NR, NM, PM, M).") |
|
run.italic = True |
|
run.font.size = Pt(font_size) |
|
|
|
def ajouter_zone_note(self): |
|
zone_note = self.document.add_table(rows=1, cols=2) |
|
zone_note.style = 'Table Grid' |
|
zone_note.alignment = WD_TABLE_ALIGNMENT.RIGHT |
|
|
|
|
|
cell_width = Cm(1.5 * self.spacing_factor) |
|
|
|
cell_libelle = zone_note.cell(0, 0) |
|
cell_libelle.width = cell_width |
|
cell_libelle.text = "Note finale" |
|
cell_libelle.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.RIGHT |
|
run = cell_libelle.paragraphs[0].runs[0] |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_table_font_size) |
|
run.font.color.rgb = RGBColor(0, 32, 96) |
|
|
|
cell_saisie = zone_note.cell(0, 1) |
|
cell_saisie.width = cell_width |
|
zone_note.rows[0].height = cell_width |
|
shading_elm = parse_xml(f'<w:shd {nsdecls("w")} w:fill="F2F2F2"/>') |
|
cell_saisie._tc.get_or_add_tcPr().append(shading_elm) |
|
cell_saisie.text = "/20" |
|
cell_saisie.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
run = cell_saisie.paragraphs[0].runs[0] |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_table_font_size) |
|
|
|
def ajouter_lignes_correcteurs(self): |
|
|
|
if len(self.elements_techniques) > 12: |
|
para = self.document.add_paragraph() |
|
para.space_before = Pt(2 * self.spacing_factor) |
|
para.space_after = Pt(2 * self.spacing_factor) |
|
run = para.add_run("Correcteurs: ") |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_font_size) |
|
para.add_run("Projet / Principal / ROV").font.size = Pt(self.dynamic_font_size) |
|
else: |
|
|
|
para_intro = self.document.add_paragraph() |
|
para_intro.space_before = Pt(2 * self.spacing_factor) |
|
|
|
for role in ["Projet", "principal", "ROV"]: |
|
para = self.document.add_paragraph() |
|
para.space_before = Pt(2 * self.spacing_factor) |
|
para.space_after = Pt(2 * self.spacing_factor) |
|
run = para.add_run(f"Correcteur {role} : ") |
|
run.bold = True |
|
run.font.size = Pt(self.dynamic_font_size) |
|
|
|
|
|
points_count = max(20, 50 - len(self.elements_techniques) * 2) |
|
para.add_run("." * points_count).font.size = Pt(self.dynamic_font_size) |
|
|
|
|
|
def modifier_centre_examen(self, nom): |
|
self.centre_examen = nom |
|
|
|
def modifier_type_examen(self, type_examen): |
|
self.type_examen = type_examen |
|
|
|
def modifier_serie(self, serie): |
|
self.serie = serie |
|
|
|
def modifier_etablissement(self, nom): |
|
self.etablissement = nom |
|
|
|
def modifier_session(self, annee): |
|
self.session = annee |
|
|
|
def modifier_candidat(self, nom): |
|
self.nom_candidat = nom |
|
|
|
def ajouter_element(self, nom, categorie, points): |
|
self.elements_techniques.append({ |
|
"nom": nom, |
|
"categorie": categorie, |
|
"points": float(points) |
|
}) |
|
|
|
def generer_document(self, nom_fichier="evaluation_gymnastique.docx"): |
|
|
|
self.calculate_dynamic_sizing() |
|
|
|
|
|
self.ajouter_entete_colore() |
|
self.creer_tableau_elements() |
|
|
|
|
|
if len(self.elements_techniques) > 15: |
|
|
|
self.ajouter_note_jury() |
|
self.creer_tableau_recapitulatif() |
|
self.ajouter_zone_note() |
|
self.ajouter_lignes_correcteurs() |
|
else: |
|
|
|
self.ajouter_note_jury() |
|
self.creer_tableau_recapitulatif() |
|
self.ajouter_lignes_correcteurs() |
|
self.ajouter_zone_note() |
|
|
|
|
|
self.ajouter_note_candidat_avec_cadre() |
|
|
|
self.document.save(nom_fichier) |
|
return nom_fichier |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
@app.route("/eps", methods=["GET", "POST"]) |
|
def index(): |
|
if request.method == "POST": |
|
|
|
centre_examen = request.form.get("centre_examen", "Centre d'examen") |
|
type_examen = request.form.get("type_examen", "Bac Général") |
|
serie = request.form.get("serie", "Série") |
|
etablissement = request.form.get("etablissement", "Établissement") |
|
session_value = request.form.get("session", "2025") |
|
nom_candidat = request.form.get("nom_candidat", "Candidat") |
|
|
|
|
|
evaluation = EvaluationGymnique() |
|
evaluation.modifier_centre_examen(centre_examen) |
|
evaluation.modifier_type_examen(type_examen) |
|
evaluation.modifier_serie(serie) |
|
evaluation.modifier_etablissement(etablissement) |
|
evaluation.modifier_session(session_value) |
|
evaluation.modifier_candidat(nom_candidat) |
|
|
|
|
|
element_names = request.form.getlist("new_element_name") |
|
element_categories = request.form.getlist("new_element_categorie") |
|
element_points = request.form.getlist("new_element_points") |
|
for name, cat, pts in zip(element_names, element_categories, element_points): |
|
if name and cat and pts: |
|
evaluation.ajouter_element(name, cat, pts) |
|
|
|
|
|
filename = "evaluation_gymnastique.docx" |
|
evaluation.generer_document(filename) |
|
|
|
|
|
output_format = request.form.get("format", "docx") |
|
if output_format == "pdf": |
|
|
|
result = convertapi.convert('pdf', { |
|
'File': filename, |
|
'FileName': 'evaluation_gymnastique', |
|
'ConvertMetadata': 'false', |
|
'ConvertHeadings': 'false' |
|
}, from_format='doc') |
|
pdf_filename = "evaluation_gymnastique.pdf" |
|
result.save_files(pdf_filename) |
|
return send_file(pdf_filename, as_attachment=True) |
|
else: |
|
return send_file(filename, as_attachment=True) |
|
|
|
return render_template("index.html") |
|
|
|
if __name__ == "__main__": |
|
app.run(debug=True) |