Reading the PDF properties/metadata in Python Reading the PDF properties/metadata in Python python python

Reading the PDF properties/metadata in Python


Try pdfminer:

from pdfminer.pdfparser import PDFParserfrom pdfminer.pdfdocument import PDFDocumentfp = open('diveintopython.pdf', 'rb')parser = PDFParser(fp)doc = PDFDocument(parser)print(doc.info)  # The "Info" metadata

Here's the output:

>>> [{'CreationDate': 'D:20040520151901-0500',  'Creator': 'DocBook XSL Stylesheets V1.52.2',  'Keywords': 'Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free',  'Producer': 'htmldoc 1.8.23 Copyright 1997-2002 Easy Software Products, All Rights Reserved.',  'Title': 'Dive Into Python'}]

For more info, look at this tutorial: A lightweight XMP parser for extracting PDF metadata in Python.


For Python 3 see PyPDF2 with example code from @Khaleel updated to:

from PyPDF2 import PdfFileReaderpdf_toread = PdfFileReader(open("test.pdf", "rb"))pdf_info = pdf_toread.getDocumentInfo()print(str(pdf_info))

Install using pip install PyPDF2.


For Python 3 and new pdfminer (pip install pdfminer3k):

import osfrom pdfminer.pdfparser import PDFParserfrom pdfminer.pdfparser import PDFDocumentfp = open("foo.pdf", 'rb')parser = PDFParser(fp)doc = PDFDocument(parser)parser.set_document(doc)doc.set_parser(parser)if len(doc.info) > 0:    info = doc.info[0]    print(info)