使用 Python 获取 PDF 版本
Getting PDF Version using Python
我需要从 PDF 文档中提取 PDF 版本。我试过 PDF miner,但它只提供以下信息:
- PDF 制作者
- 已创建
- 已修改
- 申请
下面是我试过的代码:
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 之外,还有其他库吗?
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
我需要从 PDF 文档中提取 PDF 版本。我试过 PDF miner,但它只提供以下信息:
- PDF 制作者
- 已创建
- 已修改
- 申请
下面是我试过的代码:
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 之外,还有其他库吗?
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