【发布时间】:2011-07-12 00:00:20
【问题描述】:
我正在尝试找出一种使用 Python 或 PHP 处理扫描的 pdf 的方法。我需要能够根据文本中的标识符打开多页 PDF、阅读内容并将页面移动到单个 PDF 文件(或一个文件,如果它们要分组)。
我下载并使用pdftotext 玩了一会儿,但我不确定这是否是最好的方法。我使用了一个样本扫描的 PDF,将它通过 pdftotext 运行到一个 txt 文件,然后稍微搜索了一下。它工作正常;我能够找到一些标识符,但需要 moar 正则表达式技能才能有效。但我热衷于拆分 PDF 并根据 pdftotext 移动它们。
有什么想法吗?
编辑:澄清。
- 使用 pdftotext 将 pdf 的每一页吐出到单独的 txt 文件中;
- grep txt 文件中的标识符并编译出相似页面的列表;
- 基于列表提取和组合(如果适用)相关页面,并吐出每个页面的 pdf;
- 将基于分组生成的 PDF 移动到另一个位置;
PyPDF 似乎是一个不错的起点。这是我目前所拥有的:
from pyPdf import PdfFileWriter, PdfFileReader
import re
output = PdfFileWriter()
input1 = PdfFileReader(file("test.PDF", "rb"))
totalPages = input1.getNumPages()
print "total pages to process:" +str(totalPages)
for i in range(totalPages):
p = i
print "processing page %s" %str(i)
output.addPage(input1.getPage(p))
p = input1.getPage(p).extractText()#extract text to search for identifier
pr = re.search("identifier", p)#search for the identifier; to be replaced with a list
#if there's a match, do work
if pr:
outputStream = file("test"+str(i)+".pdf", "wb")
output.write(outputStream)
outputStream.close()
print 'match on page %s' %str(i)
print '\n'
然后从这里我可以使用另一个库来根据它们的位置合并 PDF。
不过,还有一个问题:Python 的 re.search 功能有多强大?尤其是处理阴暗的OCR,能不能靠谱?
【问题讨论】:
-
Read contents of a pdf file 或 this one 的可能副本
-
这不是骗子。我在我的问题中说我已经能够阅读 PDF;您的第二个链接指向的解决方案仅部分适用于我的问题。