【问题标题】:Issues with running OCRmyPDF: A WinError2 and an Invalid Version Number运行 OCRmyPDF 的问题:WinError2 和无效的版本号
【发布时间】:2021-08-30 14:15:09
【问题描述】:

所以我正在整理一个简单的 Python 脚本来 OCR 一个 PDF:

from PIL import Image
from tika import parser
import argparse
import img2pdf
import ocrmypdf

def main():
    
    parser = argparse.ArgumentParser(description="Get text from image.")
    parser.add_argument("image_path", metavar="i", help="The path to the image being used.")
    args = parser.parse_args()
    image_path = args.image_path
    
    pdf_from_image_file_name = convert_to_pdf(image_path)
    pdf_w_ocr_file_name = ocr_pdf()
    raw_text_from_ocr_pdf = get_text_from_pdf()
    print(raw_text_from_ocr_pdf)
    
def convert_to_pdf(image_path, new_pdf_file_name="pdf_from_image"):
    temp_image = Image.open(image_path)
    pdf_bytes = img2pdf.convert(temp_image.filename)
    new_file = open('./' + new_pdf_file_name + '.pdf', 'wb')
    new_file.write(pdf_bytes)
    temp_image.close()
    new_file.close()
    return new_pdf_file_name

def ocr_pdf(pdf_file_path="./temp_pdf_file_name.pdf", new_pdf_file_name="pdf_w_ocr.pdf"):
    ocrmypdf.ocr(pdf_file_path, './'+new_pdf_file_name, deskew=True)
    return new_pdf_file_name

def get_text_from_pdf(pdf_file_path="./pdf_w_ocr.pdf"):
    raw_pdf = parser.from_file(pdf_file_path)
    return raw_pdf['content']
    
if __name__ == '__main__':
    main()

当脚本遇到import ocrmypdf 时,它会触发[WinError 2] The system cannot find the file specified 错误,但会继续过去。从 JPG 或 PNG 到 PDF 的转换工作和输出都很好。但是,当到达ocrmypdf.ocr(pdf_file_path, './'+new_pdf_file_name, deskew=True) 时,我得到了ValueError: invalid version number '4.0.0.20181030'

完整的堆栈是:

[WinError 2] The system cannot find the file specified
Traceback (most recent call last):
  File "workshop_v1.py", line 71, in <module>
    main()
  File "workshop_v1.py", line 49, in main
    pdf_w_ocr_file_name = ocr_pdf()
  File "workshop_v1.py", line 63, in ocr_pdf
    ocrmypdf.ocr(pdf_file_path, './'+new_pdf_file_name, deskew=True)
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\ocrmypdf\api.py", line 339, in ocr
    check_options(options, plugin_manager)
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\ocrmypdf\_validation.py", line 271, in check_options
    _check_options(options, plugin_manager, ocr_engine_languages)
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\ocrmypdf\_validation.py", line 266, in _check_options
    plugin_manager.hook.check_options(options=options)
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\pluggy\hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\pluggy\manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\pluggy\callers.py", line 208, in _multicall
    return outcome.get_result()
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\pluggy\callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\pluggy\callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\ocrmypdf\builtin_plugins\tesseract_ocr.py", line 84, in check_options
    version_parser=tesseract.TesseractVersion,
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\ocrmypdf\subprocess\__init__.py", line 313, in check_external_program
    if found_version and version_parser(found_version) < version_parser(need_version):
  File "C:\Users\xxx\anaconda3\envs\python37\lib\distutils\version.py", line 40, in __init__
    self.parse(vstring)
  File "C:\Users\xxx\anaconda3\envs\python37\lib\site-packages\ocrmypdf\_exec\tesseract.py", line 72, in parse
    super().parse(vstring)
  File "C:\Users\xxx\anaconda3\envs\python37\lib\distutils\version.py", line 137, in parse
    raise ValueError("invalid version number '%s'" % vstring)
ValueError: invalid version number '4.0.0.20181030'

我在装有 Windows 10 的 x64 PC 上运行它。具体来说,我通过 Anaconda 运行 Python 3.7.10 环境。 Python 中的包版本信息包括(通过pip freeze):

  • pytesseract v0.3.7
  • ocrmypdf 12.1.0
  • ghostscript v0.7

python 之外的其他可能重要的版本信息包括:

  • tesseract-ocr v4.0.0.20181030(我已经添加并尝试了许多环境变量,详情如下)
  • leptonica v1.76.0
  • ghostscript v9.54.0
  • qpdf 10.3.2(已下载,然后将文件放在C:/Windows/System32目录中)

Tesseract 安装在这里:C:\Program Files (x86)\Tesseract-OCR\,所以我尝试了以下环境变量(作为用户变量):

  • OCRMYPDF_TESSERACT = C:\Program Files (x86)\Tesseract-OCR\tesseract.exe
  • Path 末尾添加了C:\Program Files (x86)\Tesseract-OCR
  • TESSDATA_PREFIX = C:\Program Files (x86)\Tesseract-OCR\tessdata

添加指针或想法将不胜感激!

【问题讨论】:

标签: pdf ocr python-3.7 tesseract python-tesseract


【解决方案1】:

根据我在此处打开的问题在此处更新了存储库:https://github.com/jbarlow83/OCRmyPDF/issues/795

要安装使用:pip3 install pip install git+https://github.com/jbarlow83/OCRmyPDF.git#egg=ocrmypdf

我仍然收到[WinError 2] The system cannot find the file specified,但它有效,所以我现在不打算质疑它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多