有没有办法逐行读取PDF文件?



我有一个超过100页的pdf文件。有文本框和列。当我使用 PyPdf2 和 tika 解析器提取文本时,我得到一串乱序的数据。在许多情况下,它按列排序,在其他情况下跳过文档。是否可以从顶部开始阅读pdf文件,从左到右直到底部?我想阅读列和框中的文本,但我希望文本行显示为从左到右阅读。

我试过: PyPDF2 - 唯一的工具是 extracttext()。快速但不会在元素中产生间隙。结果混乱不堪。

Pdfminer - PDFPageInterpeter() 方法与 LAParams.这很好用,但很慢。每页至少 2 秒,我有 200 页。

pdfrw - 这只告诉我页数。

tabula_py - 只给我第一页。也许我没有正确循环它。

蒂卡 - 我目前正在使用什么。快速且更具可读性,但内容仍然混乱。

from tkinter import filedialog
import os
from tika import parser
import re
# select the file you want 
file_path = filedialog.askopenfilename(initialdir=os.getcwd(),filetypes=[("PDF files", "*.pdf")])
print(file_path) # print that path
file_data = parser.from_file(file_path) # Parse data from file
text = file_data['content'] # Get files text content
by_page = text.split('... Information') # split up the document into pages by string that always appears on the
# top of each page
for i in range(1,len(by_page)): # loop page by page
info = by_page[i] # get one page worth of data from the pdf
reformated = info.replace("n", "&") # I replace the new lines with     "&" to make it more readable
print("Page: ",i) # print page number
print(reformated,"nn") # print the text string from the pdf

这提供了某种输出,但它没有按照我想要的方式排序。我希望从左到右阅读pdf。另外,如果我能得到一个纯粹的python解决方案,那将是一个奖励。我不希望我的最终用户被迫安装 java(我认为 tika 和 tabula-py 方法依赖于 java)。

我用这段代码.docx这样做了。其中 txt 是.docx。希望这个帮助链接

import re
pttrn = re.compile(r'(.|?|!)('|")?s')
new = re.sub(pttrn, r'12nn', txt)
print(new)

最新更新