【问题标题】:Batch splitting PDFs by first table of contents level?按第一个目录级别批量拆分 PDF?
【发布时间】:2014-05-13 10:54:48
【问题描述】:

我正在寻找从 PDF 中提取文本以执行数据挖掘任务。 我正在查看的 PDF 包含多个报告,每个报告在文档目录中都有自己的第一级条目。此外,PDF 的开头有一个书面目录,其中包含每个报告的页码(“从页到页”)。

我正在寻找一种方法:

  • 将 PDF 拆分为单独的报告,以便将每个报告转储到 .txt 文件中。

  • 将 PDF 的每个部分直接转储为 .txt。

到目前为止,我已经能够使用 PDFminer (python) 将整个文件转储到 .txt 中,如下所示:

# Not all imports are needed for this task 
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
from cStringIO import StringIO

def myparse(data):

    fp = file(data, 'rb')
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    # Create a PDF interpreter object.
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    # Process each page contained in the document.

    for page in PDFPage.get_pages(fp):
        interpreter.process_page(page)
    #fp.close()
    #device.close()
    str = retstr.getvalue()
    #retstr.close()
    return str

t1 = myparse("part2.pdf")
text_file = open("part2.txt", "w")
text_file.write(t1)
text_file.close()

此外,这会返回目录的整个结构:

# Open a PDF document.
fp = open('solar.pdf', 'rb')
parser = PDFParser(fp)
password = ""
document = PDFDocument(parser, password)

# Get the outlines of the document.
outlines = document.get_outlines()
for (level,title,dest,a,se) in outlines:
print (level, title, a)

知道如何从这里继续前进吗?任何使用 python、R 或 bash 的工具对我个人来说都是最容易使用的,但只要它能够根据文档的第一个大纲级别进行批量拆分,任何解决方案都会很棒。

谢谢你, 马蒂亚斯

【问题讨论】:

    标签: python text text-extraction pdfminer


    【解决方案1】:

    我使用 sejda-console 找到了一个简单的解决方案:

    from subprocess import call
    import os
    
    pdfname = "example.pdf"
    
    
    outdir = "C:\\out\\%s" % pdfname
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    
    
    
    sejda = 'C:\\sejda\\bin\\sejda-console.bat'
    call = sejda
    call += ' splitbybookmarks'
    call += ' --bookmarkLevel 1'
    call += ' -f "%s"' % pdfname
    call += ' -o "%s"' % outdir
    print '\n', call
    subprocess.call(call)
    print "PDFs have been written to out-directory"
    

    显然这需要 sejda 程序:http://www.sejda.org/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-22
      • 2012-08-23
      • 1970-01-01
      • 2020-02-03
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      相关资源
      最近更新 更多