File size: 1,923 Bytes
826e4da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import datetime
import os
import tkinter as tk
from tkinter import filedialog

from PIL import Image

from src.ocr_utils import process_image_tesseract, process_image_easyocr, \
    extract_text_from_docx, process_pdf

if __name__ == "__main__":
    root = tk.Tk()
    root.withdraw()

    file_path = filedialog.askopenfilename(title="Select Input File")

    if not file_path:
        print("No file selected. Exiting...")
    else:
        output_folder = "output_files"
        os.makedirs(output_folder, exist_ok=True)

        extracted_text = ""

        if file_path.lower().endswith('.docx'):
            extracted_text = extract_text_from_docx(file_path)
        elif file_path.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff')):
            image = Image.open(file_path)
            extracted_text = process_image_tesseract(image) + process_image_easyocr(image)
            if extracted_text:
                print("Extracted Text:\n", extracted_text)
        elif file_path.lower().endswith('.pdf'):
            try:
                poppler_path = r"C:\Program Files (x86)\poppler-23.08.0\Library\bin"
                extracted_text = process_pdf(file_path, poppler_path, output_folder)
            except Exception as e:
                print(f"An error occurred during PDF processing: {e}")
        else:
            print(f"Unsupported file format: {file_path}")

        if extracted_text:
            try:
                current_datetime = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
                txt_filename = f"extracted_text_{current_datetime}.txt"
                with open(os.path.join(output_folder, txt_filename), "w", encoding="utf-8") as txt_file:
                    txt_file.write(extracted_text)
                print("Extraction completed. Text saved as:", txt_filename)
            except Exception as e:
                print("Error while saving extracted content:", str(e))