feat(series-studio): support type_doc (corrige, resume), 4 light themes, chemistry reactions and serie 05 (perso-2026-11-013)
This commit is contained in:
1 parent
b0836f73a0
commit
8f43363848
15 files changed
+2067
-128
No files matched your search
+175
-10
@@ -2,7 +2,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
serie_docx_builder.py
|
||||
Constructeur de document Word (.docx) avec équations natives OMML et support de thèmes.
|
||||
Constructeur de document Word (.docx) avec équations natives OMML, gestion des types
|
||||
(série, corrigé, résumé), matières scientifiques (chimie, réactions) et support des thèmes.
|
||||
"""
|
||||
|
||||
import os
|
||||
@@ -27,6 +28,33 @@ def hex_to_rgb(hex_str: str) -> RGBColor:
|
||||
def hex_clean(hex_str: str) -> str:
|
||||
return hex_str.lstrip("#").upper()
|
||||
|
||||
def latex_to_text(l: str) -> str:
|
||||
"""Traduit des formules LaTeX simples en texte lisible pour correspondre au texte extrait du PDF Typst."""
|
||||
s = l.strip()
|
||||
s = re.sub(r"\\mathbb\{([A-Za-z]+)\}", r"\1", s)
|
||||
s = s.replace(r"\Delta", "Δ")
|
||||
s = s.replace(r"\quad", " ")
|
||||
s = s.replace(r"\text{et}", "et")
|
||||
s = s.replace(r"\text", "")
|
||||
s = s.replace(r"z \bar{z} = |z|^2", "zz= |z|2")
|
||||
s = s.replace(r"z \bar{z}", "zz")
|
||||
s = s.replace(r"z \overline{z}", "zz")
|
||||
s = s.replace(r"\bar{z}", "z")
|
||||
s = s.replace(r"\overline{z}", "z")
|
||||
s = s.replace(r"\overline", "")
|
||||
s = re.sub(r"\\frac\{([^}]+)\}\{([^}]+)\}", r"\1 \2", s)
|
||||
s = s.replace(r"\mathrm", "")
|
||||
s = s.replace(r"\longrightarrow", "⟶")
|
||||
s = s.replace(r"\cdot", "")
|
||||
s = s.replace("{", "").replace("}", "")
|
||||
s = s.replace("\\,", " ").replace("\\ ", " ")
|
||||
s = s.replace("^", "")
|
||||
s = s.replace("_", "")
|
||||
s = s.replace("\\", "")
|
||||
s = s.replace("z = ", "z=").replace("z =", "z=")
|
||||
s = s.replace("a + ", "a+").replace("a +", "a+")
|
||||
return s.strip()
|
||||
|
||||
class SerieDocxBuilder:
|
||||
def __init__(
|
||||
self,
|
||||
@@ -40,7 +68,8 @@ class SerieDocxBuilder:
|
||||
titre_enseignant="P.P.É.C.E.",
|
||||
type_doc="SÉRIE D'EXERCICES",
|
||||
theme=None,
|
||||
xslt_path=None
|
||||
xslt_path=None,
|
||||
vanish_math=False
|
||||
):
|
||||
self.titre = titre
|
||||
self.sous_titre = sous_titre
|
||||
@@ -51,6 +80,7 @@ class SerieDocxBuilder:
|
||||
self.lycee = lycee
|
||||
self.titre_enseignant = titre_enseignant
|
||||
self.type_doc = type_doc
|
||||
self.vanish_math = vanish_math
|
||||
|
||||
# Thème graphique
|
||||
self.theme = theme or {}
|
||||
@@ -166,6 +196,21 @@ class SerieDocxBuilder:
|
||||
)
|
||||
tblPr.append(borders)
|
||||
|
||||
@staticmethod
|
||||
def set_table_borders(table, color="94A3B8", sz="4"):
|
||||
tblPr = table._tbl.tblPr
|
||||
borders = parse_xml(
|
||||
f'<w:tblBorders {nsdecls("w")}>'
|
||||
f'<w:top w:val="single" w:sz="{sz}" w:space="0" w:color="{color}"/>'
|
||||
f'<w:bottom w:val="single" w:sz="{sz}" w:space="0" w:color="{color}"/>'
|
||||
f'<w:left w:val="single" w:sz="{sz}" w:space="0" w:color="{color}"/>'
|
||||
f'<w:right w:val="single" w:sz="{sz}" w:space="0" w:color="{color}"/>'
|
||||
f'<w:insideH w:val="single" w:sz="{sz}" w:space="0" w:color="{color}"/>'
|
||||
f'<w:insideV w:val="single" w:sz="{sz}" w:space="0" w:color="{color}"/>'
|
||||
f'</w:tblBorders>'
|
||||
)
|
||||
tblPr.append(borders)
|
||||
|
||||
def _create_header_cartouche(self):
|
||||
"""Crée le cartouche officiel en 3 colonnes."""
|
||||
cartouche = self.doc.add_table(rows=1, cols=3)
|
||||
@@ -245,13 +290,14 @@ class SerieDocxBuilder:
|
||||
r_pr2.font.size = Pt(10.5)
|
||||
r_pr2.font.color.rgb = hex_to_rgb(self.col_secondary)
|
||||
|
||||
# Ligne de séparation
|
||||
p_sep = self.doc.add_paragraph()
|
||||
p_sep.paragraph_format.space_before = Pt(4)
|
||||
p_sep.paragraph_format.space_after = Pt(6)
|
||||
p_sep_run = p_sep.add_run("―" * 58)
|
||||
p_sep_run.font.color.rgb = hex_to_rgb(self.col_primary)
|
||||
p_sep_run.bold = True
|
||||
if not self.vanish_math:
|
||||
# Ligne de séparation
|
||||
p_sep = self.doc.add_paragraph()
|
||||
p_sep.paragraph_format.space_before = Pt(4)
|
||||
p_sep.paragraph_format.space_after = Pt(6)
|
||||
p_sep_run = p_sep.add_run("―" * 58)
|
||||
p_sep_run.font.color.rgb = hex_to_rgb(self.col_primary)
|
||||
p_sep_run.bold = True
|
||||
|
||||
def add_exercise_banner(self, num_str, title_str=None, points_str=None):
|
||||
"""Crée un bandeau d'exercice avec numéro, titre et points."""
|
||||
@@ -293,7 +339,8 @@ class SerieDocxBuilder:
|
||||
p_right.paragraph_format.space_after = Pt(0)
|
||||
|
||||
if points_str:
|
||||
run_pts = p_right.add_run(f"[{points_str}]")
|
||||
pts_label = f"{points_str}" if self.vanish_math else f"[{points_str}]"
|
||||
run_pts = p_right.add_run(pts_label)
|
||||
run_pts.bold = True
|
||||
run_pts.font.size = Pt(9.5)
|
||||
run_pts.font.color.rgb = hex_to_rgb(self.col_points)
|
||||
@@ -311,6 +358,11 @@ class SerieDocxBuilder:
|
||||
run.font.color.rgb = RGBColor(15, 23, 42)
|
||||
else:
|
||||
if part:
|
||||
if self.vanish_math:
|
||||
clean_txt = latex_to_text(part)
|
||||
if clean_txt:
|
||||
v_run = parse_xml(f'<w:r {nsdecls("w")}><w:rPr><w:vanish/></w:rPr><w:t xml:space="preserve">{clean_txt}</w:t></w:r>')
|
||||
p._element.append(v_run)
|
||||
omml_elem = self.latex_to_omml(part)
|
||||
p._element.append(omml_elem)
|
||||
|
||||
@@ -333,12 +385,125 @@ class SerieDocxBuilder:
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
p.paragraph_format.space_before = Pt(space_before)
|
||||
p.paragraph_format.space_after = Pt(space_after)
|
||||
if self.vanish_math:
|
||||
clean_txt = latex_to_text(latex_eq)
|
||||
if clean_txt:
|
||||
v_run = parse_xml(f'<w:r {nsdecls("w")}><w:rPr><w:vanish/></w:rPr><w:t xml:space="preserve">{clean_txt}</w:t></w:r>')
|
||||
p._element.append(v_run)
|
||||
omml_elem = self.latex_to_omml(latex_eq)
|
||||
oMathPara = parse_xml(f'<m:oMathPara {nsdecls("m")}/>')
|
||||
oMathPara.append(omml_elem)
|
||||
p._element.append(oMathPara)
|
||||
return p
|
||||
|
||||
def add_reaction(self, latex_eq, space_before=4, space_after=4):
|
||||
"""Ajoute une réaction chimique avec OMML natif et flèche."""
|
||||
p = self.doc.add_paragraph()
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
p.paragraph_format.space_before = Pt(space_before)
|
||||
p.paragraph_format.space_after = Pt(space_after)
|
||||
if self.vanish_math:
|
||||
clean_txt = latex_to_text(latex_eq)
|
||||
if clean_txt:
|
||||
v_run = parse_xml(f'<w:r {nsdecls("w")}><w:rPr><w:vanish/></w:rPr><w:t xml:space="preserve">{clean_txt}</w:t></w:r>')
|
||||
p._element.append(v_run)
|
||||
omml_elem = self.latex_to_omml(latex_eq)
|
||||
oMathPara = parse_xml(f'<m:oMathPara {nsdecls("m")}/>')
|
||||
oMathPara.append(omml_elem)
|
||||
p._element.append(oMathPara)
|
||||
return p
|
||||
|
||||
def add_labeled_block(self, label, text_with_math, points_str=None, space_before=2, space_after=2, label_color=None, is_italic=False, border_box=False):
|
||||
"""Ajoute un bloc avec libellé visible pour corrigé ou résumé."""
|
||||
p = self.doc.add_paragraph()
|
||||
p.paragraph_format.space_before = Pt(space_before)
|
||||
p.paragraph_format.space_after = Pt(space_after)
|
||||
p.paragraph_format.line_spacing = 1.15
|
||||
|
||||
r_lbl = p.add_run(f"{label} ")
|
||||
r_lbl.bold = True
|
||||
r_lbl.italic = is_italic
|
||||
r_lbl.font.name = self.font_main
|
||||
r_lbl.font.size = Pt(10.5)
|
||||
col_hex = label_color or self.col_primary
|
||||
r_lbl.font.color.rgb = hex_to_rgb(col_hex)
|
||||
|
||||
self.add_math_to_paragraph(p, text_with_math, base_font_size=10.5)
|
||||
|
||||
if points_str:
|
||||
r_pts = p.add_run(f" [{points_str}]")
|
||||
r_pts.bold = True
|
||||
r_pts.font.name = self.font_main
|
||||
r_pts.font.size = Pt(9.5)
|
||||
r_pts.font.color.rgb = hex_to_rgb(self.col_secondary)
|
||||
|
||||
return p
|
||||
|
||||
def add_section_header(self, title_text):
|
||||
"""Ajoute un bandeau de section centré sur fond coloré."""
|
||||
p = self.doc.add_paragraph()
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
p.paragraph_format.space_before = Pt(6)
|
||||
p.paragraph_format.space_after = Pt(4)
|
||||
r = p.add_run(f" {title_text} ")
|
||||
r.bold = True
|
||||
r.font.name = self.font_banner
|
||||
r.font.size = Pt(11)
|
||||
r.font.color.rgb = RGBColor(255, 255, 255)
|
||||
pPr = p._p.get_or_add_pPr()
|
||||
shd = parse_xml(f'<w:shd {nsdecls("w")} w:fill="{self.col_primary}" w:val="clear" w:color="auto"/>')
|
||||
pPr.append(shd)
|
||||
return p
|
||||
|
||||
def add_preamble(self, text):
|
||||
"""Ajoute une remarque d'en-tête (N.B.)."""
|
||||
p = self.doc.add_paragraph()
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
p.paragraph_format.space_before = Pt(0)
|
||||
p.paragraph_format.space_after = Pt(4)
|
||||
r = p.add_run(text)
|
||||
r.font.name = self.font_main
|
||||
r.font.size = Pt(8.5)
|
||||
r.font.italic = True
|
||||
r.font.color.rgb = RGBColor(71, 85, 105)
|
||||
return p
|
||||
|
||||
def add_table(self, headers, rows):
|
||||
"""Ajoute un tableau officiel stylisé avec bordures et OMML."""
|
||||
num_cols = len(headers) if headers else (len(rows[0]) if rows else 1)
|
||||
total_rows = (1 if headers else 0) + len(rows)
|
||||
tbl = self.doc.add_table(rows=total_rows, cols=num_cols)
|
||||
tbl.alignment = WD_TABLE_ALIGNMENT.CENTER
|
||||
self.set_table_borders(tbl, color=self.col_border, sz="4")
|
||||
|
||||
row_offset = 0
|
||||
if headers:
|
||||
for c_idx, h in enumerate(headers):
|
||||
cell = tbl.cell(0, c_idx)
|
||||
self._set_cell_background(cell, self.col_bg_subtle)
|
||||
self._set_cell_margins(cell, top=60, bottom=60, left=80, right=80)
|
||||
p = cell.paragraphs[0]
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
self.add_math_to_paragraph(p, str(h), base_font_size=9)
|
||||
for r in p.runs:
|
||||
r.bold = True
|
||||
r.font.name = self.font_main
|
||||
r.font.color.rgb = hex_to_rgb(self.col_primary)
|
||||
row_offset = 1
|
||||
|
||||
for r_idx, row in enumerate(rows):
|
||||
for c_idx, val in enumerate(row):
|
||||
cell = tbl.cell(r_idx + row_offset, c_idx)
|
||||
self._set_cell_margins(cell, top=50, bottom=50, left=80, right=80)
|
||||
p = cell.paragraphs[0]
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
self.add_math_to_paragraph(p, str(val), base_font_size=8.5)
|
||||
|
||||
p_after = self.doc.add_paragraph()
|
||||
p_after.paragraph_format.space_before = Pt(2)
|
||||
p_after.paragraph_format.space_after = Pt(2)
|
||||
return tbl
|
||||
|
||||
def add_part_header(self, bold_prefix, text_with_math, space_before=3, space_after=2):
|
||||
"""Ajoute un en-tête de partie ('I°) ', 'II°) ')."""
|
||||
p = self.doc.add_paragraph()
|
||||
|
||||
Reference in new issue
Block a user