python-docx-correction/Test Correction.ipynb

314 lines
8.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "396f0618",
"metadata": {},
"outputs": [],
"source": [
"from docx import Document\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "04f66d2f",
"metadata": {},
"outputs": [],
"source": [
"# Opens a student's document\n",
"# the real documents are not put for preserving student privacy :)\n",
"d = Document()\n",
"# opens raw and final document for tests\n",
"d_raw = Document(\"handout/B/brut.docx\")\n",
"d_correct = Document(\"handout/B/resultat.docx\")\n"
]
},
{
"cell_type": "markdown",
"id": "e4e7cb93",
"metadata": {},
"source": [
"### La coupure des mots automatique est activée sur le document\n",
"\n",
"Hyphen cannot be checked with API for this module"
]
},
{
"cell_type": "markdown",
"id": "5fde9202",
"metadata": {},
"source": [
"### Le style normal a été modifié de sorte à avoir un alignement de paragraphe en mode justifié"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5a14713",
"metadata": {},
"outputs": [],
"source": [
"from docx.enum.text import WD_ALIGN_PARAGRAPH\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afe924a3",
"metadata": {},
"outputs": [],
"source": [
"def check_normal_style_justified(doc):\n",
" is_correct = (\n",
" doc.styles[\"Normal\"].paragraph_format.alignment == WD_ALIGN_PARAGRAPH.JUSTIFY\n",
" )\n",
" print((\"\" if is_correct else \"not \") + \"correctly modified style\")\n",
" return is_correct\n",
"\n",
"\n",
"assert not check_normal_style_justified(d_raw)\n",
"assert check_normal_style_justified(d_correct)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58c68fbf",
"metadata": {},
"outputs": [],
"source": [
"check_normal_style_justified(d)\n"
]
},
{
"cell_type": "markdown",
"id": "bd761013",
"metadata": {},
"source": [
"### Les styles de Titre ont été appliqués comme présenté dans le document résultat"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a3dcc3a",
"metadata": {},
"outputs": [],
"source": [
"def check_titles(doc):\n",
" is_correct = True\n",
" for style in [\"Heading 1\", \"Heading 2\", \"Heading 3\"]:\n",
" nb_par_w_style = sum(\n",
" [1 if p.style.name == style else 0 for p in doc.paragraphs]\n",
" )\n",
" true_nb_par_w_style = sum(\n",
" [1 if p.style.name == style else 0 for p in d_correct.paragraphs]\n",
" )\n",
" is_correct = is_correct and (nb_par_w_style == true_nb_par_w_style)\n",
" if not is_correct:\n",
" print(\"Not all titles have been correctly inserted\")\n",
" h1_page_break = (\n",
" doc.styles[\"Heading 1\"].paragraph_format.page_break_before\n",
" if \"Heading 1\" in doc.styles\n",
" else False\n",
" )\n",
" if not h1_page_break:\n",
" print(\"Missing the page break before Title 1\")\n",
" is_correct = is_correct and h1_page_break\n",
" print((\"\" if is_correct else \"not \") + \"correctly modified titles\")\n",
" return is_correct\n",
"\n",
"\n",
"assert not check_titles(d_raw)\n",
"assert check_titles(d_correct)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a6cb02c",
"metadata": {},
"outputs": [],
"source": [
"check_titles(d)\n"
]
},
{
"cell_type": "markdown",
"id": "f005c807",
"metadata": {},
"source": [
"### La page de garde est insérée et les propriétés du document ont été modifiées en adéquation\n",
"\n",
"Il n'est pas possible de vérifier l'insertion de la page de garde avec cet API. Cependant nous pouvons vérifier la modification des propriétés du document."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "860f0680",
"metadata": {},
"outputs": [],
"source": [
"def check_properties(doc):\n",
" # student modified document Title\n",
" is_correct = doc.core_properties.title != d_raw.core_properties.title\n",
" # student modified document's author\n",
" is_correct = (\n",
" is_correct and doc.core_properties.author != d_raw.core_properties.author\n",
" )\n",
" print((\"\" if is_correct else \"not \") + \"correctly modified properties\")\n",
" return is_correct\n",
"\n",
"\n",
"assert not check_properties(d_raw)\n",
"assert check_properties(d_correct)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f24f7131",
"metadata": {},
"outputs": [],
"source": [
"check_properties(d)\n"
]
},
{
"cell_type": "markdown",
"id": "7c227f69",
"metadata": {},
"source": [
"### Il y a une bordure de page orange sur le document\n",
"\n",
"This cannot be checked with the given API"
]
},
{
"cell_type": "markdown",
"id": "1a88f8fd",
"metadata": {},
"source": [
"### Le premier paragraphe a une trame de couleur comme le présente le document résultat\n",
"\n",
"This cannot be checked with the given API"
]
},
{
"cell_type": "markdown",
"id": "176b4659",
"metadata": {},
"source": [
"### Le paragraphe de l'avant dernier chapitre a une bordure et le paragraphe a un retrait à gauche et à droite\n",
"\n",
"This cannot be checked with the given API"
]
},
{
"cell_type": "markdown",
"id": "332a3892",
"metadata": {},
"source": [
"### Les images sont insérées avec le bon habillage pour correspondre au document (carré et dessous-dessous), elles ne débordent pas sur la marge et ont une distance au texte modifiée\n",
"\n",
"This cannot be checked with the given API"
]
},
{
"cell_type": "markdown",
"id": "ec812d19",
"metadata": {},
"source": [
"### Dans la partie formulaire de contact, des tabulations séparent les éléments et ces dernières sont réglées avec des taquets personnalisé décorés de points de suite"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1104e888",
"metadata": {},
"outputs": [],
"source": [
"from docx.enum.text import WD_TAB_LEADER\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e81408d",
"metadata": {},
"outputs": [],
"source": [
"def check_tabs(doc):\n",
" is_correct = True\n",
" for par_index in [-1, -2]:\n",
" # assert that for last 2 paragraphs, we have enough tab stops\n",
" is_correct = is_correct and (\n",
" len(d_correct.paragraphs[par_index].paragraph_format.tab_stops)\n",
" == len(doc.paragraphs[par_index].paragraph_format.tab_stops)\n",
" )\n",
" # assert we have non-default tab leaders on last 2 paragraphs\n",
" is_correct = is_correct and all(\n",
" t.leader != WD_TAB_LEADER.SPACES\n",
" for t in doc.paragraphs[par_index].paragraph_format.tab_stops\n",
" )\n",
" print((\"\" if is_correct else \"not \") + \"correctly modified tabs\")\n",
" return is_correct\n",
"\n",
"\n",
"assert not check_tabs(d_raw)\n",
"assert check_tabs(d_correct)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "366cfcf1",
"metadata": {},
"outputs": [],
"source": [
"check_tabs(d)\n"
]
},
{
"cell_type": "markdown",
"id": "5a8efb33",
"metadata": {},
"source": [
"### Le pied de page a été modifié de sorte à insérer le nom de l'utilisateur.trice avec un champ et les numéros de page sont correctement insérés sur la droite du pied de page\n",
"\n",
"This cannot be checked with the given API"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.13 ('venv': venv)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
},
"vscode": {
"interpreter": {
"hash": "9bb156aafdd7e51cb3202c278380f4a2d2a5c60df2cee9d6d319c6d606de59b6"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}