在 python 中对多页 pdf 进行 ocr

ocr a multipage pdf in python

我正在使用 pytesseract 对图像进行 OCR。我有 3-4 页长的声明 pdf。我需要一种方法将它们转换成多个 .jpg/.png 图像并在这些图像上一个一个地进行 OCR。 截至目前,我正在将单个页面转换为图像,然后 运行

text=str(pytesseract.image_to_string(Image.open("imagename.jpg"),lang='eng'))

之后我使用正则表达式提取信息并创建数据框。所有页面的正则表达式逻辑都相同。可以理解的是,如果我可以循环读取图像文件,那么对于任何格式相同的 pdf 都可以自动执行该过程。

对我来说以下作品

from wand.api import library
from wand.image import Image
with Image(filename=r"imagepath.pdf", resolution=300) as img:


    library.MagickResetIterator(img.wand)
    for idx in range(library.MagickGetNumberImages(img.wand)):
        library.MagickSetIteratorIndex(img.wand, idx)

    img.save(filename="output.tiff")

现在的问题是读取 tiff file.Because 中的每一页,如果我提取为

text=str(pytesseract.image_to_string(Image.open("test.tiff"),lang='eng'))

它只会对第一页进行 OCR

PyMuPDF 将是您循环浏览图像文件的另一种选择。以下是实现此目标的方法:

import fitz
from PIL import Image
import pytesseract 

input_file = 'path/to/your/pdf/file'
pdf_file = input_file
fullText = ""

doc = fitz.open(pdf_file) # open pdf files using fitz bindings 
### ---- If you need to scale a scanned image --- ###
zoom = 1.2 # scale your pdf file by 120%
mat = fitz.Matrix(zoom, zoom)
noOfPages = doc.pageCount 

for pageNo in range(noOfPages):
    page = doc.loadPage(pageNo) # number of pages
    pix = page.getPixmap(matrix = mat) # if you need to scale a scanned image
    output = '/path/to/save/image/files' + str(pageNo) + '.jpg'
    pix.writePNG(output) # skip this if you don't need to render a page

    text = str(((pytesseract.image_to_string(Image.open(output)))))
    fullText += text

fullText = fullText.splitlines() # or do something here to extract information using regex

根据您想如何处理 pdf 文件,它非常方便。有关 PyMuPDF 的更多详细信息,这些链接可能会有所帮助:tutorial on PyMuPDF and git for PyMuPDF

希望这对您有所帮助。

编辑 使用 PyMuPDF 的另一种更直接的方法是直接解释反向转换的文本,如果你有一个干净的 PDF 文件格式,在 page = doc.loadPage(pageNo) 之后只需执行以下操作就足够了:

blocks = page.getText("blocks")
blocks.sort(key=lambda block: block[3])  # sort by 'y1' values

for block in blocks:
    print(block[4])  # print the lines of this block

免责声明:上述使用 blocks 的想法来自回购维护者。可以在此处找到更详细的信息:issues discussion on git