Aqu*_*fan 17 pdf ocr batch-processing
以前曾经问过,但我真的不知道这些答案对我有帮助.这是我的问题:我得到了一堆(10,000左右)pdf文件.有些是使用adobe的打印功能保存的文本文件(所以他们的文本是完美的,我不想冒险搞砸它们).有些是扫描图像(所以他们没有任何文字,我将不得不接受OCR).文件在同一目录中,我无法分辨哪个是哪个.最终我想将它们转换为.txt文件,然后对它们进行字符串处理.所以我希望最准确的OCR成为可能.
似乎有人建议:
我也是编程的n00b所以如果要花几周的时间来学习如何做,我宁愿支付$$$.感谢输入/体验.
顺便说一下,我正在运行Linux Mint 11 64位和/或Windows 7 64位.
以下是其他主题:
https://superuser.com/questions/107678/batch-ocr-for-many-pdf-files-not-already-ocred
只是简单地说出你的一些误解......
"我没有acrobat的许可副本,所以我不知道如何将10,000个文件转换为tiff."
你可以在免费(如在自由)和免费(如啤酒)Ghostscript的帮助下将PDF转换为TIFF.您可以选择在Linux Mint上还是在Windows 7上执行此操作.Linux的命令行是:
gs \
-o input.tif \
-sDEVICE=tiffg4 \
input.pdf
Run Code Online (Sandbox Code Playgroud)
"我不希望将10,000个30页文档变成30,000个单独的tiff图像"
您可以轻松拥有"多页"TIFF.上面的命令确实创建了G4(传真tiff)风格的这种TIFF .如果你甚至想要单页TIFF,你可以修改命令:
gs \
-o input_page_%03d.tif \
-sDEVICE=tiffg4 \
input.pdf
Run Code Online (Sandbox Code Playgroud)
该%03d输出文件名的一部分会自动转化成一系列001,002,003等.
注意事项:
tiffg4输出设备的默认分辨率为204x196 dpi.你可能想要更好的价值.要获得720 dpi,您应该添加-r720x720到命令行.-gXxY在设备点中设置widthxheight.因此,要在横向上获取ISO A4输出页面尺寸,您可以添加-g8420x5950参数.因此,控制这两个参数的完整命令,在纵向方向上在A4上产生720 dpi输出,将显示为:
gs \
-o input.tif \
-sDEVICE=tiffg4 \
-r720x720 \
-g5950x8420 \
input.pdf
Run Code Online (Sandbox Code Playgroud)
想通过回答我自己的问题我会尝试做出贡献(为自己编写了一些很好的代码,如果没有这个板的帮助就无法完成它).如果你在unix中使用pdf文件(好吧,对我来说是osx),那么有文本的pdf文件中会有单词"Font"(作为字符串,但与其他文本混合在一起)b/c那是怎么回事文件告诉Adobe要显示的字体.
bash中的cat命令似乎与在python中以二进制模式读取文件具有相同的输出(在打开文件而不是'w'或'r'或'a'时使用'rb'模式).所以我假设所有包含文本的pdf文件在二进制输出中都有"Font"字样,并且不会出现只有图像的文件.如果总是如此,则此代码将列出单个目录中包含文本的所有pdf文件以及仅包含图像的单独列表.它将每个列表保存到单独的.txt文件中,然后您可以使用bash中的命令将pdf文件移动到相应的文件夹中.
一旦将它们放在自己的文件夹中,就可以在images_only文件夹中的pdf文件上运行批处理ocr解决方案.我还没有那么远(显然).
import os, re
#path is the directory with the files, other 2 are the names of the files you will store your lists in
path = 'C:/folder_with_pdfs'
files_with_text = open('files_with_text.txt', 'a')
image_only_files = open('image_only_files.txt', 'a')
#have os make a list of all files in that dir for a loop
filelist = os.listdir(path)
#compile regular expression that matches "Font"
mysearch = re.compile(r'.*Font.*', re.DOTALL)
#loop over all files in the directory, open them in binary ('rb'), search that binary for "Font"
#if they have "Font" they have text, if not they don't
#(pdf does something to understand the Font type and uses this word every time the pdf contains text)
for pdf in filelist:
openable_file = os.path.join(path, pdf)
cat_file = open(openable_file, 'rb')
usable_cat_file = cat_file.read()
#print usable_cat_file
if mysearch.match(usable_cat_file):
files_with_text.write(pdf + '\n')
else:
image_only_files.write(pdf + '\n')
Run Code Online (Sandbox Code Playgroud)
要移动文件,我在bash shell中输入了以下命令:
cat files_with_text.txt | while read i; do mv $i Volumes/hard_drive_name/new_destination_directory_name; done
Run Code Online (Sandbox Code Playgroud)
另外,我没有重新运行上面的python代码,我只是手工编辑了这个东西,所以它可能是bug,Idk.
| 归档时间: |
|
| 查看次数: |
20513 次 |
| 最近记录: |