Home>

I'm a beginner in Python, I bought a book, and I'm currently learning while looking at information on the Web.
While looking at the information on the Web, use OCR for multiple image files saved in the folder screenshot, and output the results as one file (.txt, .word) or one file and another ocred screenshot. I'm trying, but it doesn't work.

Error message


Exception occurred: AttributeError
'str' object has no attribute'save'
File "C: \ Users \ admin \ Documents \ Python \ ocr_eng_test.py", line 24, in
text.save (os.path.join (new_dir_name, file))

Corresponding source code
import os
from PIL import Image
import pyocr
#Add the path of the installed Tesseract-OCR to the environment variable "PATH".
If i set it in #OS itself, the following two lines are unnecessary
path ='C: \\ Program Files \\ Tesseract-OCR'
os.environ ['PATH'] = os.environ ['PATH'] + path
Specify the OCR engine to be used for #pyocr in Tesseract.
pyocr.tesseract.TESSERACT_CMD = r'C: \ Program Files \ Tesseract-OCR \ tesseract.exe'
tools = pyocr.get_available_tools ()
tool = tools [0]
dir_name = "screenshot"
new_dir_name = "ocred screenshot"
files = os.listdir (dir_name)
for file in files:
    img = Image.open (os.path.join (dir_name, file))
    builder = pyocr.builders.TextBuilder (tesseract_layout = 6)
    text = tool.image_to_string (img, lang = "eng", builder = builder)
    text.save (os.path.join (new_dir_name, file))
What I tried

part of text.save (os.path.join (new_dir_name, file))
print (text)
I changed it to and confirmed that all image files are translated.
From the error result, I understand that the syntax and grammar are incorrect, but I would appreciate it if you could give me some advice.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.
Windows10, python3.9.1

  • Answer # 1

    I haven't investigated it in detail,

    The part of text.save (os.path.join (new_dir_name, file))

      with open (os.path.join (new_dir_name, file), "w") as f:
            f.write (text)


    Shouldn't it be changed to?

    If you want to change the name from .jpg to .txt, do the following:

      with open (os.path.join (new_dir_name, file) [: -3] + "txt", "w") as f:
            f.write (text)