如何使用Python读取存储在PDF文件中的属性/元数据,如标题、作者、主题和关键字?
发布于 2013-01-08 14:22:12
试试pdfminer
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
fp = open('diveintopython.pdf', 'rb')
parser = PDFParser(fp)
doc = PDFDocument(parser)
print(doc.info) # The "Info" metadata
下面是输出:
>>> [{'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'}]
有关更多信息,请参阅本教程:A lightweight XMP parser for extracting PDF metadata in Python。
发布于 2016-10-08 19:31:15
有关Python3,请参阅@Khaleel中的PyPDF2示例代码,更新为:
from PyPDF2 import PdfFileReader
pdf_toread = PdfFileReader(open("test.pdf", "rb"))
pdf_info = pdf_toread.getDocumentInfo()
print(str(pdf_info))
使用pip install PyPDF2
进行安装。
发布于 2013-01-08 16:49:01
Morten Zilmer指出: pyPdf homepage说它不再被维护。
我已经使用pyPdf实现了这一点。请看下面的示例代码。
from pyPdf import PdfFileReader
pdf_toread = PdfFileReader(open("doc2.pdf", "rb"))
pdf_info = pdf_toread.getDocumentInfo()
print(str(pdf_info))
输出:
{'/Title': u'Microsoft Word - Agnico-Eagle - Complaint (00040197-2)', '/CreationDate': u"D:20111108111228-05'00'", '/Producer': u'Acrobat Distiller 10.0.0 (Windows)', '/ModDate': u"D:20111108112409-05'00'", '/Creator': u'PScript5.dll Version 5.2.2', '/Author': u'LdelPino'}
https://stackoverflow.com/questions/14209214
复制相似问题