Eps / app.py
Docfile's picture
Update app.py
aadb4f8 verified
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
# Configuration des identifiants ConvertAPI
convertapi.api_credentials = 'secret_8wCI6pgOP9AxLVJG'
# --- Classe de génération de document ---
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)
# Informations d'en-tête par défaut
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"
# Liste vide pour les éléments techniques
self.elements_techniques = []
self.appreciations = ["M", "PM", "NM", "NR"]
def ajouter_entete_colore(self):
header_paragraph = self.document.add_paragraph()
header_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
header_paragraph.space_after = Pt(6)
header_run = header_paragraph.add_run("ÉVALUATION GYMNASTIQUE")
header_run.bold = True
header_run.font.size = Pt(14)
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
for row in header_table.rows:
row.height = Cm(0.8)
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)
# Première ligne
cell = header_table.cell(0, 0)
paragraph = cell.paragraphs[0]
run = paragraph.add_run("Centre d'examen: ")
run.bold = True
run.font.size = Pt(10)
run.font.color.rgb = RGBColor(0, 32, 96)
paragraph.add_run(self.centre_examen).font.size = Pt(10)
cell = header_table.cell(0, 1)
paragraph = cell.paragraphs[0]
run = paragraph.add_run("Examen: ")
run.bold = True
run.font.size = Pt(10)
run.font.color.rgb = RGBColor(0, 32, 96)
paragraph.add_run(self.type_examen).font.size = Pt(10)
# Deuxième ligne
cell = header_table.cell(1, 0)
paragraph = cell.paragraphs[0]
run = paragraph.add_run("Série: ")
run.bold = True
run.font.size = Pt(10)
run.font.color.rgb = RGBColor(0, 32, 96)
paragraph.add_run(self.serie).font.size = Pt(10)
cell = header_table.cell(1, 1)
paragraph = cell.paragraphs[0]
run = paragraph.add_run("Établissement: ")
run.bold = True
run.font.size = Pt(10)
run.font.color.rgb = RGBColor(0, 32, 96)
paragraph.add_run(self.etablissement).font.size = Pt(10)
# Troisième ligne
cell = header_table.cell(2, 0)
paragraph = cell.paragraphs[0]
run = paragraph.add_run("Session: ")
run.bold = True
run.font.size = Pt(10)
run.font.color.rgb = RGBColor(0, 32, 96)
paragraph.add_run(self.session).font.size = Pt(10)
cell = header_table.cell(2, 1)
paragraph = cell.paragraphs[0]
run = paragraph.add_run("Candidat: ")
run.bold = True
run.font.size = Pt(10)
run.font.color.rgb = RGBColor(0, 32, 96)
paragraph.add_run(self.nom_candidat).font.size = Pt(10)
self.document.add_paragraph().space_after = Pt(4)
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
# Configuration des largeurs de colonnes
for cell in table.columns[0].cells:
cell.width = Cm(8)
for cell in table.columns[1].cells:
cell.width = Cm(3)
for cell in table.columns[2].cells:
cell.width = Cm(2)
for cell in table.columns[3].cells:
cell.width = Cm(2.5)
for cell in table.columns[4].cells:
cell.width = Cm(2.5)
for row in table.rows:
row.height = Cm(1)
# En-tête du tableau
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(9)
run.font.color.rgb = RGBColor(0, 32, 96)
table.cell(0, 1).merge(table.cell(0, 2))
# Ajout des éléments techniques
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(9)
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(9)
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(9)
run.italic = True
def ajouter_note_jury(self):
para = self.document.add_paragraph()
para.space_before = Pt(4)
para.space_after = Pt(4)
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(8)
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)
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)
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(8)
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(8)
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(8)
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(8)
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(9)
for col, type_lettre in enumerate(["A", "B", "C", "D", "E"]):
idx = col * 2
if idx < len(note_table.columns):
neg_cell = note_table.cell(2, idx)
neg_cell.text = ""
neg_cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
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]
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(6)
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
# Définir une largeur fixe pour les deux cellules
cell_width = Cm(1.5)
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(9)
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(9)
# Ajouter plus d'espace avant la liste des correcteurs
self.document.add_paragraph().space_after = Pt(5)
self.document.add_paragraph().space_after = Pt(5)
def ajouter_lignes_correcteurs(self):
para_intro = self.document.add_paragraph()
para_intro.space_before = Pt(4)
for role in ["Projet", "principal", "ROV"]:
para = self.document.add_paragraph()
para.space_before = Pt(2)
para.space_after = Pt(2)
run = para.add_run(f"Correcteur {role} : ")
run.bold = True
para.add_run(".................................................................")
# Méthodes de modification des données
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.ajouter_entete_colore()
self.creer_tableau_elements()
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
# --- Application Flask ---
app = Flask(__name__)
@app.route("/eps", methods=["GET", "POST"])
def index():
if request.method == "POST":
# Récupération des informations depuis le formulaire
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")
# Création et configuration du document
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)
# Récupération des éléments techniques ajoutés dynamiquement
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)
# Génération du document DOCX
filename = "evaluation_gymnastique.docx"
evaluation.generer_document(filename)
# Vérification du format de sortie demandé
output_format = request.form.get("format", "docx")
if output_format == "pdf":
# Conversion en PDF via ConvertAPI
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)