【问题标题】:Reading a PDF Portfolio in Python?在 Python 中阅读 PDF 包?
【发布时间】:2017-05-26 14:13:42
【问题描述】:

我有一个 pdf 作品集,它由一个电子邮件线程组成,每封电子邮件都包含附件。我想阅读每封电子邮件中的文本并提取附件。但是,我找不到有关如何在 python 中阅读 pdf 投资组合的信息。我曾尝试使用库、PDFMiner 和 textract,但输出只是显示:“为了获得最佳体验,请在 Acrobat X 或 Adob​​e Reader X 或更高版本中打开此 PDF 组合。立即获取 Adob​​e Reader!”

有什么想法吗?谢谢!

【问题讨论】:

    标签: python python-2.7 pdf


    【解决方案1】:

    使用@Roland Smith 和@ikreb 的答案从投资组合中提取嵌入文件需要我做很多工作。 python-poppler 有一个相当神秘的 API,上面的说明只是获取数据,而不是 pdf。以下步骤详细说明了如何使用 poppler 和 python 子流程从投资组合中获取文档:

    1. 您需要安装 poppler。它可以在 Mac 上与 homebrew(或 condo)一起安装。您可能还需要 cmake(也与自制软件一起安装)。以下是在 Windows 上安装的多种方式:How to install Poppler on Windows?

    2. Poppler 是一个命令行程序,因此您不必使用 python 来解决您的问题。从命令行:

    # this will pull the files from the portfolio and save them to the same directory
    pdfdetach -saveall <file_name, no quotes>
    
    # example:
    pdfdetach -saveall my_portfolio.pdf
    
    1. 在python中,使用子进程如下:
    import subprocess
    
    # pdfdetach will save all files from the portfolio to the same directory
    subprocess.run(['pdfdetach', '-saveall', file_name.pdf])
    
    # if you want to get a list of the files, use -list (see note below)
    subprocess.run(['pdfdetach', '-list', file_name])
    
    # it is also useful, within a script, to save to another folder using -o:
    subprocess.run(['pdfdetach', '-saveall', os.path.join(os.path.join(os.getcwd(), my_portfolio.pdf), '-o', os.path.join(os.getcwd(), './out')])
    
    
    

    关于列表输出的注意事项:输出将是一个子进程对象,必须对其进行解析以生成 python 文件名列表。这篇文章有几种方法可以做到这一点:python subprocess output to list or file

    【讨论】:

      【解决方案2】:

      你可以使用python-poppler

      from poppler import load_from_file
      
      pdf_document = load_from_file("portfolio.pdf")
      
      if pdf_document.has_embedded_files():
          for attachment in pdf_document.embedded_files():
              print(attachment.data)
      

      【讨论】:

        【解决方案3】:

        来自poppler 实用程序的程序pdfdetach 可以提取附件。

        大多数类 UNIX 操作系统发行版都有可用的 poppler-utils 软件包。您可以在 SourceForge 上找到 ms-windows 版本。

        您可以使用subprocess 模块从 Python 调用此程序。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-20
          • 1970-01-01
          • 2018-01-29
          • 2023-03-07
          • 1970-01-01
          • 2010-12-29
          • 1970-01-01
          相关资源
          最近更新 更多