如何将使用 Tika 从 PDF 中提取的文本放入 JSON?
Posted
技术标签:
【中文标题】如何将使用 Tika 从 PDF 中提取的文本放入 JSON?【英文标题】:How to put in a JSON the texts extracted from a PDF with Tika? 【发布时间】:2021-10-08 15:32:46 【问题描述】:我想知道是否可以将使用 Tika Python 从 PDF 中提取的文本放入 JSON,以便将来我可以将它们导入系统的相应记录中。下面是我用来从 PDF 返回解析文本的代码。
from tika import parser
def extract_text(file):
parsed = parser.from_file(file)
parsed_text = parsed['content']
return parsed_text
file_name_with_extension = input("Enter File Name:")
text = extract_text(file_name_with_extension)
print(text)
【问题讨论】:
【参考方案1】:你是这个意思吗:
from tika import parser
import json
def extract_text(file):
parsed = parser.from_file(file)
parsed_text = json.dumps(parsed_pdf['metadata'] , indent = 2)
return parsed_text
text = extract_text('Untitled.pdf')
print(text)
输出:
"Content-Type": "application/pdf",
"Creation-Date": "2021-07-31T12:15:55Z",
"Last-Modified": "2021-07-31T12:15:55Z",
"Last-Save-Date": "2021-07-31T12:15:55Z",
"X-Parsed-By": [
"org.apache.tika.parser.DefaultParser",
"org.apache.tika.parser.pdf.PDFParser"
],
"X-TIKA:content_handler": "ToTextContentHandler",
"X-TIKA:embedded_depth": "0",
"X-TIKA:parse_time_millis": "26",
"access_permission:assemble_document": "true",
"access_permission:can_modify": "true",
"access_permission:can_print": "true",
"access_permission:can_print_degraded": "true",
"access_permission:extract_content": "true",
"access_permission:extract_for_accessibility": "true",
"access_permission:fill_in_form": "true",
"access_permission:modify_annotations": "true",
"created": "2021-07-31T12:15:55Z",
"date": "2021-07-31T12:15:55Z",
"dc:format": "application/pdf; version=1.3",
"dc:title": "Untitled",
"dcterms:created": "2021-07-31T12:15:55Z",
"dcterms:modified": "2021-07-31T12:15:55Z",
"meta:creation-date": "2021-07-31T12:15:55Z",
"meta:save-date": "2021-07-31T12:15:55Z",
"modified": "2021-07-31T12:15:55Z",
"pdf:PDFVersion": "1.3",
"pdf:charsPerPage": "1393",
"pdf:docinfo:created": "2021-07-31T12:15:55Z",
"pdf:docinfo:creator_tool": "Pages",
"pdf:docinfo:modified": "2021-07-31T12:15:55Z",
"pdf:docinfo:producer": "",
"pdf:docinfo:title": "Untitled",
"pdf:encrypted": "false",
"pdf:hasMarkedContent": "true",
"pdf:hasXFA": "false",
"pdf:hasXMP": "false",
"pdf:unmappedUnicodeCharsPerPage": "0",
"producer": "",
"resourceName": "b'Untitled.pdf'",
"title": "Untitled",
"xmp:CreatorTool": "Pages",
"xmpTPg:NPages": "1"
【讨论】:
几乎需要文本在 JSON 中,而不是 PDF 数据中。不知道你能不能看懂……我写错了问题,我会改正的! 但无论如何,它已经以某种方式有所帮助,不仅对我有帮助,也许对其他人也有帮助。以上是关于如何将使用 Tika 从 PDF 中提取的文本放入 JSON?的主要内容,如果未能解决你的问题,请参考以下文章