使用@Roland Smith 和@ikreb 的答案从投资组合中提取嵌入文件需要我做很多工作。 python-poppler 有一个相当神秘的 API,上面的说明只是获取数据,而不是 pdf。以下步骤详细说明了如何使用 poppler 和 python 子流程从投资组合中获取文档:
-
您需要安装 poppler。它可以在 Mac 上与 homebrew(或 condo)一起安装。您可能还需要 cmake(也与自制软件一起安装)。以下是在 Windows 上安装的多种方式:How to install Poppler on Windows?
-
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
- 在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