Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,109 +1,128 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
-
import zipfile
|
| 4 |
import shutil
|
|
|
|
| 5 |
import tempfile
|
| 6 |
-
|
|
|
|
| 7 |
import pandas as pd
|
|
|
|
|
|
|
| 8 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
def extract_text_from_pdf(pdf_path):
|
| 16 |
-
text = ""
|
| 17 |
-
with fitz.open(pdf_path) as doc:
|
| 18 |
-
for page in doc:
|
| 19 |
-
text += page.get_text()
|
| 20 |
-
return text
|
| 21 |
|
| 22 |
def process_pdfs(files):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
for file in files:
|
| 33 |
-
if zipfile.is_zipfile(file.name):
|
| 34 |
-
with zipfile.ZipFile(file.name, 'r') as zip_ref:
|
| 35 |
-
zip_ref.extractall(temp_dir)
|
| 36 |
-
pdf_files = [os.path.join(dp, f) for dp, dn, filenames in os.walk(temp_dir) for f in filenames if f.lower().endswith(".pdf")]
|
| 37 |
-
else:
|
| 38 |
-
output_path = os.path.join(temp_dir, os.path.basename(file.name))
|
| 39 |
-
shutil.copy(file.name, output_path)
|
| 40 |
-
pdf_files = [output_path]
|
| 41 |
-
|
| 42 |
-
for pdf_path in pdf_files:
|
| 43 |
-
filename = os.path.basename(pdf_path)
|
| 44 |
-
text = extract_text_from_pdf(pdf_path)
|
| 45 |
-
|
| 46 |
-
# Extraction nom
|
| 47 |
-
name_match = title_pattern.search(text)
|
| 48 |
-
nom_prenom = "NO_NAME"
|
| 49 |
-
safe_name = "NO_NAME"
|
| 50 |
-
if name_match:
|
| 51 |
-
nom_prenom = name_match.group(0).strip()
|
| 52 |
-
safe_name = re.sub(r"[^\w]", "_", nom_prenom)
|
| 53 |
-
safe_name = re.sub(r"_+", "_", safe_name)
|
| 54 |
-
safe_name = re.sub(r"(_?DEPARTEMENT|_?ACTION|_?DIRECTION|_?SERVICE|_?UNITE|_?DIVISION)+", "", safe_name, flags=re.IGNORECASE)
|
| 55 |
-
safe_name = safe_name.strip("_")
|
| 56 |
-
|
| 57 |
-
# Date
|
| 58 |
-
date_match = seance_pattern.search(text)
|
| 59 |
-
date_str = "NO_DATE"
|
| 60 |
-
if date_match:
|
| 61 |
-
day, month, year = date_match.groups()
|
| 62 |
-
date_str = f"{year}{month}{day}"
|
| 63 |
-
folder_path = os.path.join(pdf_folder, date_str)
|
| 64 |
-
os.makedirs(folder_path, exist_ok=True)
|
| 65 |
-
else:
|
| 66 |
-
folder_path = pdf_folder
|
| 67 |
-
|
| 68 |
-
# Référence DI
|
| 69 |
-
ref_match = ref_pattern.search(text)
|
| 70 |
-
ref_code = "NO_REF"
|
| 71 |
-
ref_excel = "NO_REF"
|
| 72 |
-
if ref_match:
|
| 73 |
-
ref_year, ref_number = ref_match.groups()
|
| 74 |
-
ref_code = f"{ref_year}-{ref_number}"
|
| 75 |
-
ref_excel = f"{ref_year}/{ref_number}"
|
| 76 |
-
|
| 77 |
-
# Nom de fichier
|
| 78 |
-
final_filename = f"{safe_name}_{ref_code}.pdf"
|
| 79 |
-
final_path = os.path.join(folder_path, final_filename)
|
| 80 |
-
|
| 81 |
-
shutil.copy(pdf_path, final_path)
|
| 82 |
-
|
| 83 |
-
# Pour Excel
|
| 84 |
-
excel_data.append([
|
| 85 |
-
filename,
|
| 86 |
-
final_filename,
|
| 87 |
-
nom_prenom,
|
| 88 |
-
ref_excel,
|
| 89 |
-
date_str
|
| 90 |
-
])
|
| 91 |
-
|
| 92 |
-
# Générer Excel
|
| 93 |
-
output_excel = os.path.join(temp_dir, "résultats.xlsx")
|
| 94 |
-
df = pd.DataFrame(excel_data, columns=["Fichier_Base", "FichierNouveau", "Nom et Prénom", "DI", "Date de séance"])
|
| 95 |
-
df.to_excel(output_excel, index=False)
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
btn_process = gr.Button("2. Lancer le traitement")
|
| 104 |
-
output_excel = gr.File(label="3. Télécharger l'Excel", file_types=[".xlsx"])
|
| 105 |
|
| 106 |
-
btn_process.click(fn=process_pdfs, inputs=input_files, outputs=output_excel)
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
|
|
|
| 3 |
import shutil
|
| 4 |
+
import zipfile
|
| 5 |
import tempfile
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from datetime import datetime
|
| 8 |
import pandas as pd
|
| 9 |
+
import concurrent.futures
|
| 10 |
+
|
| 11 |
import gradio as gr
|
| 12 |
+
from PyPDF2 import PdfReader
|
| 13 |
+
|
| 14 |
+
# Expressions régulières
|
| 15 |
+
title_pattern = re.compile(r"\b(?:Monsieur|Madame)\s+([A-ZÉÈÎÔÂÙÛÄËÏÖÜ][\w\s\-']+)", re.IGNORECASE)
|
| 16 |
+
ref_pattern = re.compile(r"\bDI[\s\-]?(\d+)[\s\-]?(\d+)\b")
|
| 17 |
+
seance_pattern = re.compile(r"SÉANCE\s+DU\s+(\d{2})[/-](\d{2})[/-](\d{4})", re.IGNORECASE)
|
| 18 |
+
|
| 19 |
+
# Dossier de sortie
|
| 20 |
+
RESULT_DIR = "results"
|
| 21 |
+
os.makedirs(RESULT_DIR, exist_ok=True)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def extract_info_from_pdf(pdf_path, output_folder):
|
| 25 |
+
try:
|
| 26 |
+
pdf_reader = PdfReader(pdf_path)
|
| 27 |
+
text = ""
|
| 28 |
+
for page in pdf_reader.pages:
|
| 29 |
+
text += page.extract_text() or ""
|
| 30 |
+
|
| 31 |
+
original_name = os.path.basename(pdf_path)
|
| 32 |
+
name_match = title_pattern.search(text)
|
| 33 |
+
safe_name = "NO_NAME"
|
| 34 |
+
if name_match:
|
| 35 |
+
name = name_match.group(1).strip()
|
| 36 |
+
safe_name = re.sub(r"[^\w]", "_", name)
|
| 37 |
+
safe_name = re.sub(r"_+", "_", safe_name)
|
| 38 |
+
safe_name = re.sub(r"(_?DEPARTEMENT|_?ACTION|_?DIRECTION|_?SERVICE|_?UNITE|_?DIVISION)+", "", safe_name, flags=re.IGNORECASE)
|
| 39 |
+
safe_name = safe_name.strip("_")
|
| 40 |
+
else:
|
| 41 |
+
name = "Inconnu"
|
| 42 |
+
|
| 43 |
+
ref_match = ref_pattern.search(text)
|
| 44 |
+
ref_code = f"{ref_match.group(1)}-{ref_match.group(2)}" if ref_match else "NO_REF"
|
| 45 |
+
ref_excel = f"{ref_match.group(1)}/{ref_match.group(2)}" if ref_match else "NO_REF"
|
| 46 |
|
| 47 |
+
date_match = seance_pattern.search(text)
|
| 48 |
+
if date_match:
|
| 49 |
+
day, month, year = date_match.groups()
|
| 50 |
+
date_str = f"{year}{month}{day}"
|
| 51 |
+
date_excel = f"{day}/{month}/{year}"
|
| 52 |
+
else:
|
| 53 |
+
date_str = "NO_DATE"
|
| 54 |
+
date_excel = "NO_DATE"
|
| 55 |
+
|
| 56 |
+
# Nouveau nom de fichier
|
| 57 |
+
new_filename = f"{safe_name}_{ref_code}_{date_str}.pdf"
|
| 58 |
+
new_path = os.path.join(output_folder, new_filename)
|
| 59 |
+
shutil.copy(pdf_path, new_path)
|
| 60 |
+
|
| 61 |
+
return {
|
| 62 |
+
"Fichier_Base": original_name,
|
| 63 |
+
"FichierNouveau": new_filename,
|
| 64 |
+
"Nom et Prénom": name,
|
| 65 |
+
"DI": ref_excel,
|
| 66 |
+
"Date de séance": date_excel,
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Erreur lors du traitement de {pdf_path} : {e}")
|
| 71 |
+
return {
|
| 72 |
+
"Fichier_Base": os.path.basename(pdf_path),
|
| 73 |
+
"FichierNouveau": "Erreur",
|
| 74 |
+
"Nom et Prénom": "Erreur",
|
| 75 |
+
"DI": "Erreur",
|
| 76 |
+
"Date de séance": "Erreur"
|
| 77 |
+
}
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
def process_pdfs(files):
|
| 81 |
+
output_dir = RESULT_DIR
|
| 82 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 83 |
+
|
| 84 |
+
pdf_paths = []
|
| 85 |
+
|
| 86 |
+
# Extraire les fichiers s’il y a un zip
|
| 87 |
+
for f in files:
|
| 88 |
+
if f.name.endswith(".zip"):
|
| 89 |
+
with zipfile.ZipFile(f.name, 'r') as zip_ref:
|
| 90 |
+
tmp_dir = tempfile.mkdtemp()
|
| 91 |
+
zip_ref.extractall(tmp_dir)
|
| 92 |
+
for path in Path(tmp_dir).rglob("*.pdf"):
|
| 93 |
+
pdf_paths.append(str(path))
|
| 94 |
+
elif f.name.endswith(".pdf"):
|
| 95 |
+
pdf_paths.append(f.name)
|
| 96 |
+
|
| 97 |
+
# Traitement en parallèle
|
| 98 |
+
records = []
|
| 99 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
|
| 100 |
+
futures = [executor.submit(extract_info_from_pdf, path, output_dir) for path in pdf_paths]
|
| 101 |
+
for future in concurrent.futures.as_completed(futures):
|
| 102 |
+
records.append(future.result())
|
| 103 |
+
|
| 104 |
+
# Sauvegarder Excel
|
| 105 |
+
df = pd.DataFrame(records)
|
| 106 |
+
output_excel = os.path.join(output_dir, "résultat.xlsx")
|
| 107 |
+
df.to_excel(output_excel, index=False)
|
| 108 |
|
| 109 |
+
return output_excel, RESULT_DIR
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
with gr.Blocks(title="Traitement de fichiers PDF") as demo:
|
| 113 |
+
gr.Markdown("## 📄 Traitement de fichiers PDF et génération Excel")
|
| 114 |
+
input_files = gr.File(label="1. Fichiers PDF ou ZIP", file_types=[".zip", ".pdf"], file_count="multiple")
|
| 115 |
+
btn_doc = gr.Button("2. Lancer le traitement")
|
| 116 |
+
output_excel = gr.File(label="📊 Fichier Excel", file_types=[".xlsx"])
|
| 117 |
+
output_folder = gr.File(label="📁 Dossier contenant les fichiers renommés", file_types=[".pdf"], file_count="multiple")
|
| 118 |
|
| 119 |
+
def run_all(files):
|
| 120 |
+
excel_path, folder = process_pdfs(files)
|
| 121 |
+
pdfs = list(Path(folder).glob("*.pdf"))
|
| 122 |
+
return excel_path, pdfs
|
| 123 |
|
| 124 |
+
btn_doc.click(run_all, inputs=[input_files], outputs=[output_excel, output_folder])
|
|
|
|
|
|
|
| 125 |
|
|
|
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|