5

I'm looking for a very fast, lightweight Python library to read PDF metadata. I don't need any write capabilities. It would be better if only the metadata information is loaded, not the entire file.

I realise an interpreted language like Python isn't the best choice for speed, but as this solution needs to be cross platform and work with an existing Python application there doesn't seem to be much of a choice.

I checked out pyPdf and some other libraries, but am ideally looking for something lighter and faster, suitable for processing tens of thousands of files in one go.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
ianaré
  • 3,230
  • 26
  • 26
  • What, if anything, makes pyPdf too heavy? If you only need to write, do not import PdfWriter. – bdd Dec 09 '10 at 23:05
  • The lib itself is rather light, but it reads the entire file. This means processing larger PDFs takes longer, in the case of huge files like manuals it is very slow. – ianaré Dec 10 '10 at 09:14

4 Answers4

3

pdfrw can read the metadata without reading parsing the entire file. (Disclaimer: I am the author of pdfrw.) For example:

>>> from pdfrw import PdfReader
>>> PdfReader('pdf_reference_1-7.pdf').Info
{'/Title': '(PDF Reference, version 1.7)',
 '/CreationDate': '(D:20061017081020Z)',
 '/Producer': '(Acrobat Distiller 7.0.5 \\(Windows\\))',
 '/Creator': '(FrameMaker 7.2)',
 '/ModDate': "(D:20061118211043-02'30')",
 '/Author': '(Adobe Systems Incorporated)',
 '/Subject': '(Adobe Portable Document Format \\(PDF\\))'}
Patrick Maupin
  • 8,024
  • 2
  • 23
  • 42
  • Is this still true? I took a look at the source code of PdfReader and could clearly see that the entier file is read during init. See: https://github.com/pmaupin/pdfrw/blob/master/pdfrw/pdfreader.py#L493 – jrast Feb 12 '17 at 00:12
  • 1
    @jrast -- sorry, you're right -- I saw "read in" and I was thinking "parse".. It does read in the entire file in one go, but for most applications that's not a problem (obviously, memory limited systems could be different). The slowness with most PDF readers and operations is in the parsing, and the pdfrw parsing is done somewhat lazily. – Patrick Maupin Feb 13 '17 at 10:21
1

Here's something I just put together, built on top of the Python PDFMiner library. You can extract both "Info" and XMP type metadata with it.

Matt Swain
  • 3,827
  • 4
  • 25
  • 36
0

It's a little Raw, but this should get you the meta data

f = open('file.pdf', 'r')
pdfdata=f.read()
metas=re.findall('<</Metadata(.*?)>>',pdfdata)
Luke Rehmann
  • 514
  • 5
  • 12
0

Have you seen this answer to a similar question? It suggests using fopen and manually parsing the metadata. If the metadata is all you need, you can parse it yourself and make it as fast as you like.

Community
  • 1
  • 1
alexis
  • 48,685
  • 16
  • 101
  • 161