Using pytesseract on Python 2.7 and Windows XP

Viewed 325

I need OCR for a certain project, after searching online I decided to use python and tesseract. Right now I am trying to run the following code just to see if it works:

import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
print(pytesseract.image_to_string(Image.open("C:\Documents and Settings\Yerutnik\Desktop\file.bmp")))

However, I am getting the following error:

Traceback (most recent call last):
  File "C:\Documents and Settings\Yerutnik\Desktop\test1.py", line 2, in <module>
    import pytesseract
  File "C:\Python27\lib\site-packages\pytesseract\__init__.py", line 2, in <module>
    from .pytesseract import ALTONotSupported
  File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 89
    f"{tesseract_cmd} is not installed or it's not in your PATH."
                                                                ^
SyntaxError: invalid syntax

I am running this on a Windows XP 32bit machine (must use this machine), Python 2.7.9, Tesseract 4.0.0 (tested working separately in cmd, and I checked that it is in PATH).

2 Answers

you use pytesseract for python3 (f-string is python3 feature) in python2.7 Try some old version of pytesseract.

I was able to fix this by downgrading pytesseract (as suggested by user898678) from 4.0.0 to 0.2.2, upgrading pip from 1.5.2 to 20, and installing pytesseract from web instead of using a wheel file.

Related