[Python] OSError with Tesseract

Discussão em 'Python' iniciado por Stack, Julho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I want to OCR one pdf file and detect table using img2table in Jupyter. However, i can't properly execute it because of problem with OSError. It can't find Tesseract in the system despite actually being installed with path to it My code:

    import pytesseract as ps
    ps.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
    import cv2
    from pandas import DataFrame as df
    import numpy as np
    from PIL import Image
    from pdf2image import convert_from_path
    from img2table.ocr import TesseractOCR
    from img2table.document import Image as TableImage

    pdf_path = 'file.pdf'

    pages=convert_from_path(pdf_path, poppler_path=r'C:\poppler-24.08.0\Library\bin')

    for i, page_image in enumerate(pages, start=1):
    print(f"\n--- Страница {i} ---")


    img = np.array(page_image)


    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)


    pil_img = Image.fromarray(thresh)
    stamp_img=TableImage(pdf_path, detect_rotation=True)
    stamp=stamp_img.extract_tables(ocr=TesseractOCR(lang="rus"))
    text = ps.image_to_string(pil_img, lang='rus')
    print(text.strip())
    for s in stamp:
    stamp=s.df
    print(stamp)


    Error log:

    ---------------------------------------------------------------------------
    OSError Traceback (most recent call last)
    Cell In[17], line 14
    12 pil_img = Image.fromarray(thresh)
    13 stamp_img=TableImage(pdf_path, detect_rotation=True)
    ---> 14 stamp=stamp_img.extract_tables(ocr=TesseractOCR(lang="rus"))
    15 text = ps.image_to_string(pil_img, lang='rus')
    16 print(text.strip())

    File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\img2table\ocr\tesseract.py:56, in TesseractOCR.__init__(self, n_threads, lang, psm, tessdata_dir)
    54 cmd_tess = subprocess.run("tesseract --version", env=self.env, shell=True)
    55 if cmd_tess.returncode != 0:
    ---> 56 raise EnvironmentError("Tesseract not found in environment. Check variables and PATH")
    58 # Check if requested languages are available
    59 try:

    OSError: Tesseract not found in environment. Check variables and PATH


    How can i solve this problem without changing PATH

    Continue reading...

Compartilhe esta Página