【问题标题】:Manipulate PDF files (read, split, combine, move)操作 PDF 文件(读取、拆分、合并、移动)
【发布时间】:2011-07-12 00:00:20
【问题描述】:

我正在尝试找出一种使用 Python 或 PHP 处理扫描的 pdf 的方法。我需要能够根据文本中的标识符打开多页 PDF、阅读内容并将页面移动到单个 PDF 文件(或一个文件,如果它们要分组)。

我下载并使用pdftotext 玩了一会儿,但我不确定这是否是最好的方法。我使用了一个样本扫描的 PDF,将它通过 pdftotext 运行到一个 txt 文件,然后稍微搜索了一下。它工作正常;我能够找到一些标识符,但需要 moar 正则表达式技能才能有效。但我热衷于拆分 PDF 并根据 pdftotext 移动它们。

有什么想法吗?


编辑:澄清。

  1. 使用 pdftotext 将 pdf 的每一页吐出到单独的 txt 文件中;
  2. grep txt 文件中的标识符并编译出相似页面的列表;
  3. 基于列表提取和组合(如果适用)相关页面,并吐出每个页面的 pdf;
  4. 将基于分组生成的 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,能不能靠谱?

【问题讨论】:

  • 这不是骗子。我在我的问题中说我已经能够阅读 PDF;您的第二个链接指向的解决方案仅部分适用于我的问题。

标签: php python pdf


【解决方案1】:

我在小型项目中成功使用了pypdf

【讨论】:

    【解决方案2】:

    你试过 PyPdf 吗?见:http://pybrary.net/pyPdf/

    这是一个使用 PyPdf 提取文本的方法:http://code.activestate.com/recipes/511465-pure-python-pdf-to-text-converter/

    【讨论】:

      猜你喜欢
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多