首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取PDF文件python - pypdf2时出现断言错误

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

Stack Overflow用户
提问于 2020-05-21 16:11:51
回答 1查看 319关注 0票数 0

当我尝试读取PDF文件时,出现以下错误。

代码:

代码语言:javascript
复制
from PyPDF2 import PdfFileReader
import os

os.chdir("Path to dir")

pdf_document = 'sample.pdf'
pdf = PdfFileReader(pdf_document,'rb') #Error here

错误:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2020-05-21 16:33:59

我在这里写了这篇文章,它完全适用于我,pdf在同一个文件夹中,你也可以使用os来获取字符串类型的路径值。

代码语言:javascript
复制
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的方法。希望这对你解决任何问题有所帮助,我会尽力回复的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61930075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档