python - 在 Python 中从 PDF 元数据中提取关键字
问题描述
我有一个 PDF 文件,我想从它的元数据中获取一些信息。为此,我遵循以下程序:
from PyPDF2 import PdfFileReader
mypath = "your_pdf_file.pdf"
pdf_toread = PdfFileReader(open(mypath, 'rb'))
pdf_info = pdf_toread.getDocumentInfo()
对于手头的文档,输出是:
Out[230]:
{'/CrossmarkDomainExclusive': 'true',
'/CreationDate': "D:20181029074117+05'30'",
'/CrossMarkDomains#5B2#5D': 'elsevier.com',
'/Author': 'Nicola Gennaioli',
'/Creator': 'Elsevier',
'/ElsevierWebPDFSpecifications': '6.5',
'/Subject': 'Journal of Monetary Economics, 98 (2018) 98-113. doi:10.1016/j.jmoneco.2018.04.011',
'/CrossmarkMajorVersionDate': '2010-04-23',
'/CrossMarkDomains#5B1#5D': 'sciencedirect.com',
'/robots': 'noindex',
'/ModDate': "D:20181029074135+05'30'",
'/AuthoritativeDomain#5B1#5D': 'sciencedirect.com',
'/Keywords': 'Sovereign Risk; Sovereign Default; Government Bonds',
'/doi': '10.1016/j.jmoneco.2018.04.011',
'/Title': 'Banks, government Bonds, and Default: What do the data Say?',
'/AuthoritativeDomain#5B2#5D': 'elsevier.com',
'/Producer': 'Acrobat Distiller 10.1.10 (Windows)'}
然而,我发现 PyPDF2 库没有“访问”/Keywords
部件信息的属性。也就是这一点输出:
'/Keywords': 'Sovereign Risk; Sovereign Default; Government Bonds',
所以,我想要一些关于如何获取元数据输出信息的帮助[在这个例子中:Sovereign Risk; Sovereign Default; Government Bonds
]。
为了重现输出,我正在共享文档的链接
例如做
更新:
print(pdf_info.title)
Banks, government Bonds, and Default: What do the data Say?
print(pdf_info.subject)
Journal of Monetary Economics, 98 (2018) 98-113. doi:10.1016/j.jmoneco.2018.04.011
但是,当我尝试为这/Keywords
部分做类似的事情时,我得到了以下属性错误:
pdf_info.keywords
Traceback (most recent call last):
File "<ipython-input-295-3852401ef983>", line 1, in <module>
pdf_info.keywords
AttributeError: 'DocumentInformation' object has no attribute 'keywords'
解决方案
该键/Keywords
实际上存在于由 返回的字典中getDocumentInfo
,因此您不必做任何特殊的事情(除非首先测试它是否存在或将其包装在 atry
中,以防它不存在于另一个文件中):
from PyPDF2 import PdfFileReader
mypath = "../Downloads/banks_gov_bonds_default.pdf"
pdf_toread = PdfFileReader(open(mypath, 'rb'))
pdf_info = pdf_toread.getDocumentInfo()
if '/Keywords' in pdf_info:
print (pdf_info['/Keywords'])
印刷
Sovereign Risk; Sovereign Default; Government Bonds
这确实是示例 PDF 中字段中的关键字。
另一种选择是通过在您的 pip 放置它的 PYPDF2 文件夹内进行编辑来添加 keywords
到公开的 PDF 属性。pdf.py
您可以在我的版本中的第 2781 行附近的某处找到 class 中的title
、creator
和author
更多属性的创建。DocumentInformation
所有这些属性的创建都遵循一个简单的方案,因此添加一个完全没有问题:
keywords = property(lambda self: self.getText("/Keywords"))
"""Read-only property accessing the document's **producer**.
If the document was converted to PDF from another format, this is
the name of the application (for example, OSX Quartz) that converted
it to PDF. Returns a unicode string (``TextStringObject``)
or ``None`` if the producer is not specified."""
keywords_raw = property(lambda self: self.get("/Keywords"))
"""The "raw" version of producer; can return a ``ByteStringObject``."""
(我之所以添加keywords_raw
只是因为其他属性也是如此。不过,我不能直接说出这些是什么。)
之后,您的代码实际上可以工作:
from PyPDF2 import PdfFileReader
mypath = "../Downloads/banks_gov_bonds_default.pdf"
pdf_toread = PdfFileReader(open(mypath, 'rb'))
pdf_info = pdf_toread.getDocumentInfo()
print (pdf_info.keywords)
结果,再次:
Sovereign Risk; Sovereign Default; Government Bonds
推荐阅读
- java - 如何在android应用工具栏的左侧添加一个按钮?
- javascript - 如何根据另一个数组更新Javascript数组中的值?
- react-native - 通过 GraphQL 键检索 AWS Amplify DataStore 记录
- reactjs - 反应 useRef 以专注于下一个渲染的输入字段
- symfony - 如何访问 FormType 类中提交的数据?
- firebase - 如何以最基本的形式获取 Firebase onCall 和异步响应:{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}
- php - ADODB odbc_execute
- javascript - NuxtJS (Vue):如何将帖子列表放入组件或布局中?
- wordpress - 在 wordpress 中更改 index.html
- react-admin - 使用 FormDataConsumer 时表单字段的样式