简体   繁体   English

有没有一种方法可以使用Python对一个文件夹中的所有pdf文件进行OCR?

[英]Is there a way to OCR all pdf files within one folder using Python?

As the title states, is there a way to OCR all pdf files within one folder using Python? 如标题所述,有没有一种方法可以使用Python对一个文件夹中的所有pdf文件进行OCR? I have this code below, but it only OCR's one file at a time and extract text. 我在下面有此代码,但一次仅OCR的一个文件并提取文本。 I want to do a general OCR of all the pdf in a folder. 我想对文件夹中的所有pdf文件进行常规OCR。 Please let me know if its possible to do so. 请让我知道是否可以这样做。

from wand.image import Image
from PIL import Image as PI
import pyocr
import pyocr.builders
import io

tool = pyocr.get_available_tools()[0]
lang = tool.get_available_languages()[1]

req_image = []
final_text = []

image_pdf = Image(filename="./PDF_FILE_NAME", resolution=300)
image_jpeg = image_pdf.convert('jpeg')

for img in req_image: 
    txt = tool.image_to_string(
        PI.open(io.BytesIO(img)),
        lang=lang,
        builder=pyocr.builders.TextBuilder()
    )
    final_text.append(txt)

I like the glob module. 我喜欢glob模块。
You can match against a pattern for a given folder. 您可以针对给定文件夹的模式进行匹配。
Here is your code with some edits to show how it might work. 这是您的代码,并进行了一些编辑以显示其工作方式。

import glob
pdfs = glob.glob("./*.pdf")

for pdf in pdfs:
    image_pdf = Image(pdf, resolution=300)
    image_jpeg = image_pdf.convert('jpeg')
    txt = tool.image_to_string(
        PI.open(io.BytesIO(image_jpeg)),
        lang=lang,
        builder=pyocr.builders.TextBuilder()
    )
    final_text.append(txt)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法使用 windows Python 打印任何文件夹中的所有内容? - Is there a way to print all contents within any folder using windows Python? 将文件夹中的所有 pdf 文件合并到一个 pdf 与 pypdf2 - Merging all pdf files in a folder into one pdf with pypdf2 使用Python将文件夹中的所有.xls文件附加到一个.csv文件中 - Appending all .xls files in a folder into one .csv file using Python 有没有办法使用 Boto3 python 在 S3 中将具有特定文件扩展名的所有文件从一个文件夹移动到另一个文件夹? - Is there a way to move all files with specific file extensions from one folder to another in S3 using Boto3 python? 如何从python中的文件夹一一读取pdf文件 - How to read pdf files one by one from a folder in python Python - 显示特定文件夹中没有给定文件夹的所有文件 - Python - Present all files within a specific folder without the given folder 如何在python中绘制一个文件夹中的所有文件? - How to plot all files in one folder in python? 使用python比较两个文件夹中的文件 - Compare files within two folder using python 如何将目录/文件夹中的所有pdf文件转换为图像python 3? - How to convert all pdf files in a directory/folder to image python 3? 如何在多个子目录中找到具有相同扩展名的所有文件并使用 python 将它们移动到单独的文件夹中? - How can find all files of the same extension within multiple subdirectories and move them to a seperate folder using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM