-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
98 lines (85 loc) · 3.38 KB
/
Copy pathmain.py
File metadata and controls
98 lines (85 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
import pytesseract
from pdf2image import convert_from_path
from docx import Document
from tkinter import Tk, filedialog, messagebox
from colorama import Fore, Style
import logging
from logs.logger import error, info
from ui.file_selector import select_files, select_export_location, show_info_message, show_error_message
# Configuration du logging
logging.basicConfig(
filename="lexo.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
def ocr_image(image_path, lang="eng"):
"""Effectue l'OCR sur une image donnee."""
try:
text = pytesseract.image_to_string(image_path, lang=lang)
info(f"OCR reussi pour l'image : {image_path}")
return text
except Exception as e:
error(f"Erreur lors de l'OCR de l'image {image_path} : {e}")
return ""
def pdf_to_images(pdf_path):
"""Convertit un PDF en une liste d'images."""
try:
images = convert_from_path(pdf_path)
info(f"Conversion PDF en images reussie : {pdf_path}")
return images
except Exception as e:
error(f"Erreur lors de la conversion du PDF {pdf_path} : {e}")
return []
def save_to_docx(text, output_path):
"""Enregistre le texte dans un fichier Word."""
try:
doc = Document()
for line in text.splitlines():
doc.add_paragraph(line)
doc.save(output_path)
info(f"Fichier Word cree : {output_path}")
except Exception as e:
error(f"Erreur lors de la creation du fichier Word {output_path} : {e}")
def process_file(file_path, lang="eng", export_folder=None, file_type="docx"):
"""Traite un fichier (image ou PDF) et génère un fichier exporté."""
try:
if file_path.lower().endswith(".pdf"):
images = pdf_to_images(file_path)
text = "\n\n".join([ocr_image(image, lang) for image in images])
else:
text = ocr_image(file_path, lang)
if export_folder:
output_path = os.path.join(export_folder, os.path.basename(os.path.splitext(file_path)[0]) + f".{file_type}")
else:
output_path = os.path.splitext(file_path)[0] + f".{file_type}"
if file_type == "docx":
save_to_docx(text, output_path)
else:
with open(output_path, "w", encoding="utf-8") as f:
f.write(text)
show_info_message("Succès", f"Fichier traité avec succès : {output_path}")
except Exception as e:
error(f"Erreur lors du traitement du fichier {file_path} : {e}")
show_error_message("Erreur", f"Une erreur est survenue : {e}")
def select_and_process_files():
"""Ouvre une boîte de dialogue pour sélectionner et traiter des fichiers."""
try:
file_paths = select_files()
if not file_paths:
return
export_folder = select_export_location()
lang = "fra" # Par défaut, français
file_type = "docx" # Par défaut, Word
for file_path in file_paths:
process_file(file_path, lang, export_folder, file_type)
except Exception as e:
error(f"Erreur lors de la sélection des fichiers : {e}")
show_error_message("Erreur", f"Une erreur est survenue : {e}")
def main():
"""Point d'entree principal de l'application."""
root = Tk()
root.withdraw() # Masquer la fenêtre principale
select_and_process_files()
if __name__ == "__main__":
main()