首页 > 解决方案 > 传递 pdf 文件目录以执行 OCR,并为 Python 中的每个转换文件生成 .txt 文件

问题描述

我有一个包含 pdf 文件的目录。当您将文件名传递给 wand.image 类的对象时,我编写了执行 OCR 的代码。我现在要做的是遍历pdf文件的目录并为每个pdf生成一个OCR'd txt文件并将其保存在某个目录中。我写到现在的代码如下:

import io
from PIL import Image
import pytesseract
from wand.image import Image as wi




pdf = wi(filename = r"D:\files\aba7d525-04b8-4474-a40d-e94f9656ed42.pdf", resolution = 300)

pdfImg = pdf.convert('jpeg')

imgBlobs = []

for img in pdfImg.sequence:
    page = wi(image = img)
    imgBlobs.append(page.make_blob('jpeg'))

extracted_text = []

for imgBlob in imgBlobs:
    im = Image.open(io.BytesIO(imgBlob))
    text = pytesseract.image_to_string(im, lang = 'eng')
    extracted_text.append(text)

print(extracted_text[0])

关于如何从 OCR'd pdf 生成 .txt 文件的任何建议

标签: pythonloopspdffile-handlingpython-tesseract

解决方案


在代码末尾试试这个:

with open('filename.txt', 'w') as result:
     for line in extracted_text:
          result.write(line,'\n')

推荐阅读