ianaré
ianaré

Reputation: 3298

Fast Python PDF metadata reader

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.

Upvotes: 5

Views: 7084

Answers (4)

Patrick Maupin
Patrick Maupin

Reputation: 8127

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\\))'}

Upvotes: 3

Luke Rehmann
Luke Rehmann

Reputation: 564

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)

Upvotes: 0

alexis
alexis

Reputation: 50190

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.

Upvotes: 0

Matt Swain
Matt Swain

Reputation: 3887

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.

Upvotes: 1

Related Questions