python - 传递 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 文件的任何建议
解决方案
在代码末尾试试这个:
with open('filename.txt', 'w') as result:
for line in extracted_text:
result.write(line,'\n')
推荐阅读
- graphql - GraphQL .NET - 结果中的扩展
- java - Selenium Chrome DevTools:未知错误:无法在 localhost:6666 从 chrome 连接到 chrome 无法访问
- mongoose - 如何在nestjs中填充猫鼬引用?
- python - 困难的 groupby() 和数据分析问题(Python)(如何简化)
- c++ - 为什么用 C++ std::array 创建二维数组需要额外的一对 {}
- kotlin - 嵌套片段不会回到前一个片段
- computer-vision - 高 mAP@50,精度和召回率低。这是什么意思,什么指标应该更重要?
- java - 尝试在 android 5.0 设备上的 onCreate 方法中启动新活动时应用程序崩溃
- javascript - 如何将多个参数放入 onClick 属性
- python - 用于加密的初学者凯撒密码解密过程