Skip to content

Commit 6fc5c64

Browse files
committed
Second major update
1 parent 823a10e commit 6fc5c64

20 files changed

+559
-293
lines changed

.gitignore

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
build
2-
.vscode
31
dev.txt
42
*\.ipynb_checkpoints
5-
__pycache__/
6-
TODOs.txt
7-
/source/_build
3+
__pycache__/

.vscode/settings.json

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"_restructuredtext.linter.extraArgs": [
3+
"--ignore D002",
4+
"--ignore D004"
5+
],
6+
"files.autoSave": "afterDelay",
7+
"spellright.language": [
8+
"fr"
9+
],
10+
"spellright.documentTypes": [
11+
"markdown",
12+
"latex",
13+
"plaintext"
14+
],
15+
"python.linting.enabled": false,
16+
"python.pythonPath": "C:\\ProgramData\\Miniconda3\\envs\\pythonscript_dev\\python.exe",
17+
"restructuredtext.confPath": "${workspaceFolder}\\source",
18+
"restructuredtext.builtDocumentationPath": "${workspaceFolder}\\_build",
19+
"restructuredtext.updateOnTextChanged": "true",
20+
"restructuredtext.linter.name": "rstcheck",
21+
"restructuredtext.updateDelay": 300,
22+
"restructuredtext.linter.run": "onSave",
23+
"restructuredtext.preview.name": "docutil",
24+
"cSpell.language": "en,fr",
25+
"cSpell.words": [
26+
"CONDAPATH",
27+
"ENVNAME",
28+
"ENVPATH",
29+
"INPUTFILE",
30+
"Lorsqu'un",
31+
"Matplotlib",
32+
"Numpy",
33+
"Rossum",
34+
"Youtube",
35+
"aujourd'hui",
36+
"binder",
37+
"chainage",
38+
"conda",
39+
"datascience",
40+
"envs",
41+
"geopandas",
42+
"jupyter",
43+
"kernel",
44+
"kernels",
45+
"lorsqu'il",
46+
"lorsqu'on",
47+
"manning",
48+
"miniconda",
49+
"myenv",
50+
"overflow",
51+
"parsefile",
52+
"rasterio",
53+
"scale",
54+
"someenv",
55+
"spyder",
56+
"stackoverflow",
57+
"timeit"
58+
],
59+
}

.vscode/spellright.dict

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
StackOverflow
2+
Environment
3+
conda
4+
pip
5+
Spyder
6+
Overflow
7+
stackoverflow
8+
programming
9+
scale
10+
target
11+
Lab
12+
Bucket
13+
Rossum
14+
Num
15+
Geo
16+
Rasterio
17+
Matplotlib
18+
Cheat
19+
deprecated
20+
Jupyter
21+
jupyter
22+
ipynb
23+
Youtube
24+
Markdown
25+
Automating
26+
Boring
27+
Stuff
28+
with
29+
Practical
30+
Users
31+
autocompletion
32+
py
33+
wildcard
34+
dir
35+
Rectory
36+
cls
37+
echo
38+
notepad
39+
where
40+
calc
41+
Numpy
42+
channel
43+
Miniconda
44+
activate
45+
myenv
46+
list
47+
datascience
48+
create
49+
envs
50+
name
51+
env
52+
remove
53+
deactivate
54+
yml
55+
install
56+
uninstall
57+
d'autocomplétion
58+
History
59+
chdir
60+
path
61+
workingdirectory
62+
delete
63+
toogle
64+
indent
65+
unindent
66+
completion
67+
selection
68+
Context
69+
Shortcut
70+
Space
71+
clear
72+
array
73+
builder
74+
magic
75+
commands
76+
namespace
77+
timeit
78+
sum
79+
who
80+
whos
81+
lsmagic
82+
print
83+
quickref
84+
Ipython
85+
hint
86+
quit
87+
88+
qtconsole
89+
Inline
90+
build
91+
virtualenv
92+
Find
93+
cell

dev.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Construction du tutoriel
2+
3+
## Conversion des notebooks en scripts python
4+
5+
Les notebooks servent à la création du site. Les scripts peuvent être utilisés directement par Spyder.
6+
Le package `Jupytext` permet d'effectuer la conversion avec la commande:
7+
8+
``jupytext --to py:percent --opt comment_magics=false file.ipynb``
9+
File renamed without changes.
File renamed without changes.

exercices/solution/culvert_logparser.py exercices/loganalysis/solution/culvert_logparser.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
backslashs)
1212
2. Exécuter le script avec `python culvert_logparser.py`
1313
14+
Dépendances:
15+
- python 3.7
16+
- pandas 1.0.5
17+
1418
Historique:
1519
- ML (10/09/2019): Création du script
20+
- ML (09/07/2020): MAJ de la doc
1621
"""
1722
# Les imports se placent par convention tout au début du fichier.
1823
# On importe pandas de la manière conventionnelle
@@ -21,7 +26,7 @@
2126
###############################################################################
2227
############################ Données d'entrée #################################
2328
###############################################################################
24-
CULVERT_FILE_PATH ="culvert_logfile.txt"
29+
CULVERT_FILE_PATH = "culvert_logfile.txt"
2530
###############################################################################
2631

2732
# Indique visuellement le départ de l'exécution du script.

exercices/modifpdf/shapes.csv

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id,description
2+
1,rectangle
3+
2,triangle
4+
3,cercle

exercices/modifpdf/shapes.pdf

23 KB
Binary file not shown.
25.7 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""Script pour récupérer les données d'un fichier log culvert
2+
3+
Description détaillée:
4+
Le script lit d'abord les données d'un fichier CSV. Ensuite, il ouvre un fichier
5+
PDF et ajoute à page le texte formaté extrait du fichier CSV et enregistre le nouveau
6+
fichier PDF.
7+
8+
Utilisation:
9+
1. Modifier les variables PDF_FILE_PATH et CSV_FILE_PATH pour qu'elles indiquent
10+
le chemin vers les fichiers CSV et PDF. (r"path\to\file" si le chemin contient des
11+
backslashs)
12+
2. Exécuter le script avec `python ajout_texte_pdf.py` ou F5 dans Spyder.
13+
14+
Dépendances:
15+
- python 3.7
16+
- PyMuPDF 1.17.3
17+
18+
Historique:
19+
- ML (09/07/2020): Création du script
20+
"""
21+
# Les imports se placent par convention tout au début du fichier.
22+
# Import du module CSV de la librairie standard dédié à la gestion des fichiers CSV
23+
import csv
24+
# Import du module système qui permet, entre autres, de quitter le programme
25+
import sys
26+
# Import de PyMuPDF (son nom d'import est différent)
27+
import fitz
28+
29+
###############################################################################
30+
############################ Données d'entrée #################################
31+
###############################################################################
32+
CSV_FILE_PATH = r"..\shapes.csv"
33+
PDF_FILE_PATH = r"..\shapes.pdf"
34+
OUTPUT_PDF_FILE_PATH = r"..\shapes_with_text.pdf"
35+
###############################################################################
36+
37+
# Indique visuellement le départ de l'exécution du script.
38+
print("Starting...")
39+
40+
# Création de listes vides prêtes à contenir les données du fichier CSV.
41+
shape_ids = []
42+
shape_descriptions = []
43+
44+
# Ouverture du fichier texte
45+
with open(CSV_FILE_PATH, mode="r", encoding="utf-8") as csv_file:
46+
# On utilise l'object DictReader du module CSV qui suppose que la première
47+
# ligne du fichier CSV est une ligne d'en-tête, ce qui est le cas ici.
48+
csv_reader = csv.DictReader(csv_file)
49+
# On peut itérer sur cet objet, et extraire ligne par ligne les données.
50+
for row in csv_reader:
51+
shape_ids.append(row["id"])
52+
shape_descriptions.append(row["description"])
53+
54+
55+
# On ouvre le fichier PDF dans un context manager fourni par la fonction open de PyMuPDF
56+
with fitz.open(PDF_FILE_PATH) as pdf:
57+
58+
# On vérifie ici que le nombre de page du fichier PDF correspond bien
59+
# au nombre d'entrée dans le fichier CSV. Sinon on quitte le programme.
60+
if len(pdf) != len(shape_ids):
61+
print("Le fichier CSV et le fichier PDF ne contiennent pas le même nombre d'éléments.")
62+
sys.exit() # Arrêt du script
63+
64+
# On effectue une boucle au travers des éléments du CSV et des pages du fichier PDF
65+
for shape_id, shape_description, page in zip(shape_ids, shape_descriptions, pdf):
66+
# Création du texte à rajouter, \n permet de sauter des lignes
67+
text = f"ID: {shape_id}\nType: {shape_description}\nAjouté par PyMuPDF"
68+
# Ajout du texte à chaque page.
69+
# On note que cela ne modifie pas directement le fichier ouvert, cela enregistre
70+
# dans la mémoire de nouvelles pages PDF.
71+
page.insertText(
72+
fitz.Point(100, 50), # Position du texte
73+
text,
74+
fontsize=10,
75+
color=(0, 0, 1) # bleu
76+
)
77+
78+
# Lorsque la boucle est terminée, on enregistre le fichier PDF sous un nouveau nom.
79+
output_pdf_file = r"..\shapes_with_text.pdf"
80+
pdf.save(OUTPUT_PDF_FILE_PATH)
81+
82+
# Indique visuellement que le script s'est bien executé jusqu'au bout.
83+
print("Done!")

0 commit comments

Comments
 (0)