【问题标题】:how to extract text from images in a pdf file using pytesseract如何使用pytesseract从pdf文件中的图像中提取文本
【发布时间】:2018-09-26 20:27:08
【问题描述】:

我正在尝试使用以下代码从 pdf 文件的图像中提取文本。 PDF 文件是一份合同文件,是一份合同的扫描件。 pdf文件中的所有页面都是图像。

当我尝试使用以下代码提取数据时,我收到一条错误消息,提示它无法读取文件/无法识别图像文件。

try:
    import Image
except ImportError:
    from PIL import Image

import pytesseract

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'

# Simple image to string
#print(pytesseract.image_to_string(Image.open('C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\Scripts\\1184.pdf')))

Traceback(最近一次调用最后一次):

  File "C:\Users\Administrator\eclipse-workspace\tesseract\test\greetings.py", line 18, in <module>
    print(pytesseract.image_to_string(Image.open('C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\Scripts\\1184.pdf')))
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2622, in open
    % (filename if filename else fp))
OSError: cannot identify image file 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\Scripts\\1184.pdf'

请帮我看看怎么办

【问题讨论】:

  • 发生这种情况的原因是它无法找到文件。所以路径可能是错误的(或者你应该使用 raw 字符串)。

标签: python pdf python-tesseract


【解决方案1】:

您尝试将 pdf 文件作为图像打开。他们的文档中不可能有枕头状态,他们不支持阅读 pdf 文件,请参阅:https://pillow.readthedocs.io/en/5.1.x/handbook/image-file-formats.html

无论如何,您都可以使用库 pdf2image 将 pdf 转换为图像,然后用枕头打开它并将其提供给 tesseract。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    相关资源
    最近更新 更多