我曾使用 PyPdf 从 PDF 文件中读取文本。然而,PyPDF 并不是按照 PDF 中的每一行文本来读取,它是以一种杂乱无章的方式进行读取。在 PDF 中并没有换行符的地方,它也会插入一个新的换行符。
import PyPDF2
pdf_path = r'C:\Users\PDFExample\Desktop\Temp\sample.pdf'
pdfFileObj = open(pdf_path, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
page_nos = pdfReader.numPages
for i in range(page_nos):
# Creating a page object
pageObj = pdfReader.getPage(i)
# Printing Page Number
print("Page No: ",i)
# Extracting text from page
# And splitting it into chunks of lines
text = pageObj.extractText().split(" ")
# Finally the lines are stored into list
# For iterating over list a loop is used
for i in range(len(text)):
# Printing the line
# Lines are seprated using "\n"
print(text[i],end="\n\n")
print()
这让我获得了以下内容:
Our Ref :
21
1
8
88
1
11
5
Name:
S
ky Blue
Ref 1 :
1
2
-
34
-
56789
-
2021/2
Ref 2:
F2021004
444
Amount:
$
1
00
.
11
...
预期的是
Our Ref :2118881115 Name: Sky Blue Ref 1 :12-34-56789-2021/2 Ref 2:F2021004444
Amount: $100.11 Total Paid:$0.00 Balance: $100.11 Date of A/C: 01/08/2021 Date Received: 10/12/2021
Last Paid: Amt Last Paid: A/C Status: CLOSED Collector : Sunny Jane
这是pdf文件的链接: https://pdfhost.io/v/eCiktZR2d_sample2
PyPDF
无法逐行读取文件,因为该格式实际上没有行,只有一堆看起来没有PostScript代码的字符。另外:PyPDF
最后更新于2010年。它已经有了后继者。PyPDF2
接着被PyPDF3
和PyPDF4
所取代。它们都不能真正做到你所期望的。它们在页面级别上的工作效果最佳。 - BoarGules2.3.1
的,但它也没有起作用。 - Himanshuman