使用Python获取PDF版本

2024-04-28 22:01:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从PDF文档中提取PDF版本。我尝试了PDF miner,但它仅提供以下信息:

  1. PDF制作人
  2. 创造
  3. 修改
  4. 应用

下面是我尝试的代码:

from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument

fp = open("ibs.servlets.pdf", 'rb')
parser = PDFParser(fp)
doc = PDFDocument(parser)
parser.set_document(doc)
if len(doc.info) > 0:
   info = doc.info[0]
   print(info)

除了pdf miner,还有其他库可以使用吗


Tags: from文档import版本info信息parserdoc
1条回答
网友
1楼 · 发布于 2024-04-28 22:01:23

PDF版本作为注释存储在PDF文件的第一行中。我无法找到如何使用pdfparser获取此信息,但使用PyPDF2我可以手动检索此信息:

from PyPDF2.pdf import PdfFileReader
doc = PdfFileReader('ibs.servlets.pdf')
doc.stream.seek(0) # Necessary since the comment is ignored for the PDF analysis
print(doc.stream.readline().decode())

输出:

%PDF-1.5

相关问题 更多 >