当我试图阅读一个PDF文件时,我得到了以下错误。
代码。
from PyPDF2 import PdfFileReader
import os
os.chdir("Path to dir")
pdf_document = 'sample.pdf'
pdf = PdfFileReader(pdf_document,'rb') #Error here
错误。
Traceback (most recent call last):
File "/home/krishna/PycharmProjects/sample/sample.py", line 9, in
pdf = PdfFileReader(filehandle)
File "/home/krishna/PycharmProjects/AI_DRC/venv/lib/python3.6/site-packages/PyPDF2/pdf.py", line 1084, in init
self.read(stream)
File "/home/krishna/PycharmProjects/AI_DRC/venv/lib/python3.6/site-packages/PyPDF2/pdf.py", line 1838, in read
assert start >= last_end
AssertionError
注意:文件大小为18MB
我在这里写了这个,它对我来说完全有效,pdf在同一个文件夹里,你也可以使用os来获取字符串类型的路径值。
import PyPDF2
pdf_file = PyPDF2.PdfFileReader("Sample.pdf")#addressing the file, you can use os method it works on that as well
page_content = pdf_file.getPage(0).extractText()# here I get the psge number one(index zero) and then extracted its content
print(page_content)#you can then do whatever you want with it
我认为你的程序的问题在于那个 "rb "的东西,你在正常的文件处理中使用它,PyPDF2已经有了PdfFileReader、PdfFileWriter和PdfFileMerger的方法,希望对你有所帮助如果你反驳任何问题,只需提及,我会尝试着去回复它。