我有一个包含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的pdf格式生成.txt文件的任何建议
答案 0 :(得分:1)
在代码末尾尝试以下操作:
with open('filename.txt', 'w') as result:
for line in extracted_text:
result.write(line,'\n')