繁体   English   中英

读取 PDF 文件 python - pypdf2 时出现断言错误

[英]Getting Assertion error while reading the PDF file python - pypdf2

当我尝试读取 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

注意:文件大小为 18 MB

在这里我写了这个,它完全适合我,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 的方法。 希望它有所帮助如果您解决任何问题,只需提及,我会尽力解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM