Spaces:
Sleeping
Sleeping
| import spacy | |
| import shutil | |
| import os | |
| import pdfplumber | |
| import pytesseract | |
| from PIL import Image | |
| from pdf2image import convert_from_path | |
| # Configura il percorso di tesseract | |
| pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/tesseract' | |
| # Carica il modello linguistico italiano di spaCy dal percorso specificato | |
| model_dir = "it_core_news_sm-3.7.0 2" | |
| nlp = spacy.load(model_dir) | |
| nlp.max_length = 1500000 | |
| # Mappatura delle categorie e delle relative parole chiave | |
| categorie_keywords = { | |
| "Fatture": ["fattura", "invoice", "parcella"], | |
| "Visure": ["visura"], | |
| "Contratti": ["contratto", "accordo", "incarico"], | |
| "Bilanci": ["bilancio", "bilanci", "bilancio di esercizio", "ricavi", "costi"], | |
| "Spese di Personale": ["cedolino", "paga"], | |
| "Pagamenti": ["bonifico", "transazione", "pagamento", "disposizione", "cro", "ordinante", "beneficiario"], | |
| "Preventivi": ["preventivo", "offerta", "proposta"], | |
| "DSAN": ["dichiarazione", "dsan", "dichiarazione sostitutiva", "dichiara"], | |
| "E.C.": ["estratto conto", "movimenti"] | |
| } | |
| def estrai_testo_da_pdf_con_pdfplumber(file_path): | |
| text = '' | |
| try: | |
| with pdfplumber.open(file_path) as pdf: | |
| for page in pdf.pages: | |
| try: | |
| page_text = page.extract_text() | |
| if page_text: | |
| text += page_text | |
| except Exception as e: | |
| print(f"Errore nell'estrazione del testo dalla pagina PDF: {e}") | |
| except Exception as e: | |
| print(f"Errore nell'apertura o elaborazione del file PDF: {e}") | |
| return text.lower() | |
| def estrai_testo_da_pdf_con_ocr(file_path): | |
| text = '' | |
| pages = convert_from_path(file_path) | |
| for page in pages: | |
| text += pytesseract.image_to_string(page, lang='ita') | |
| return text.lower() | |
| def estrai_testo_da_immagine(file_path): | |
| image = Image.open(file_path) | |
| return pytesseract.image_to_string(image, lang='ita').lower() | |
| def assegna_categoria_con_spacy(testo): | |
| doc = nlp(testo) | |
| for token in doc: | |
| for categoria, keywords in categorie_keywords.items(): | |
| if token.text.lower() in keywords: | |
| return categoria | |
| return None | |
| def classifica_e_sposta_documenti(cartella_origine, file_name): | |
| file_path = os.path.join(cartella_origine, file_name) | |
| testo = '' | |
| # Determina il tipo di file e estrai il testo appropriatamente | |
| if file_path.endswith('.pdf'): | |
| testo = estrai_testo_da_pdf_con_pdfplumber(file_path) | |
| if not testo: | |
| testo = estrai_testo_da_pdf_con_ocr(file_path) | |
| elif file_path.lower().endswith(('.png', '.jpg', '.jpeg')): | |
| testo = estrai_testo_da_immagine(file_path) | |
| else: | |
| print(f"Il formato del file {file_path} non è supportato.") | |
| return | |
| # Assegna la categoria con spaCy | |
| categoria = assegna_categoria_con_spacy(testo) | |
| if categoria: | |
| destination = os.path.join(cartella_origine, categoria, file_name) | |
| destination_folder = os.path.join(cartella_origine, categoria) | |
| if not os.path.exists(destination_folder): | |
| os.makedirs(destination_folder) | |
| # Controlla se il file esiste già e gestiscilo come preferisci | |
| if not os.path.exists(destination): | |
| shutil.move(file_path, destination) | |
| else: | |
| print(f"Il file {destination} esiste già.") | |
| # Qui puoi decidere di sovrascriverlo, rinominarlo, o ignorarlo. | |
| # Ad esempio, per rinominarlo: | |
| # new_destination = os.path.join(destination_folder, "new_" + file_name) | |
| # shutil.move(file_path, new_destination) | |
| else: | |
| print("Nessuna categoria trovata per il documento.") | |