【发布时间】:2021-02-06 01:39:45
【问题描述】:
我正在循环浏览一个目录并阅读大量 PDF。我正在使用循环从每个页面中提取所有文本信息。
5/13 PDF 在尝试使用 .getNumPages() 时抛出错误:发生异常:ValueError 无效的 int() 字面量为 10:b''。我认为发生此错误是因为对象(PyPDF2)显示 numPages: 0。
当前代码
dir = os.listdir(directory)
for f in dir:
object = PyPDF2.PdfFileReader(directory + '\\' + f)
NumPages = object.getNumPages()
text_output = "" # Initiate Variable
# Loop through all pages and extract/merge text
with open(directory + '\\' + f, mode='rb') as FileName:
reader = PyPDF2.PdfFileReader(FileName)
for p_num in range(0, NumPages):
page = reader.getPage(p_num)
text_output = text_output + '\n' + 'PAGE: ' + \
str(p_num + 1) + '\n' + page.extractText()
I added an image showing the object data where numPages: 0
我不明白为什么只有某些 PDF 存在这个问题。任何帮助将不胜感激!
【问题讨论】:
-
pdf 文件可能在某种程度上与普通 pdf 文件不同。您可以尝试在查看器中打开它们并再次将它们另存为 pdf 以尝试修复偏差。
-
我试过这个没有成功。
-
@alexlong,你能分享你的 pdf 吗?
标签: python text-extraction pypdf2