我的计算机上的文件夹中存储了大量 PDF 文件,我尝试从每个文件中提取元数据并使用这些信息创建一个数据框。但是,我在文件夹中遇到了一些损坏的 PDF。就我的目的而言,从这些损坏的 PDF 中提取元数据而不是简单地忽略它们将非常有用。使用Python可以实现这个吗?
大多数库存 PDF MetaInfo 可通过 xpdf 或 poppler 实用程序访问,并且通常包含在 Python 安装中。因此,如果我们在 for 循环中递归扫描所有文件,则 PDFInfo 会生成结构良好的输出,即使对于失败的文件也是如此。
>Apps\PDF\Xpdf\xpdf-tools-win-4.04\bin32\pdfinfo aa1.pdf
Syntax Error: Couldn't read xref table
Syntax Warning: PDF file is damaged - attempting to reconstruct xref table...
Title: *Untitled - Notepad
Author:
Producer: Print My PDF
CreationDate: Sun Jan 1 00:00:00 2023
ModDate: Sun Jan 1 00:00:00 2023
Tagged: no
Form: none
Pages: 1
Encrypted: no
Page size: 595.32 x 841.92 pts (A4) (rotated 0 degrees)
File size: 4416 bytes
Optimized: no
PDF version: 1.7
因此,我们需要做的就是过滤我们感兴趣的对象,例如生成文件的内容或源文档标题是什么?
现在问题仅限于Python,而我不使用它,所以你需要写很多行。我只需在 OS shell CMD 级别运行“findstr”(类似于 Unix utils)并在文件名前面添加前缀,使用批处理中的循环,以输出为例:
echo aa1.pdf & pdfinfo aa1.pdf |findstr " Title:"
aa1.pdf
Syntax Error: Couldn't read xref table
Syntax Warning: PDF file is damaged - attempting to reconstruct xref table...
Title: *Untitled - Notepad