我写了一个快速的脚本来计算PDF文件中的页数。但是,在某些文件中,它无法读取,并返回一个错误:Xref table not Zero indexed。基本上,当需要一个对象Id(5.0)和获取对象Id(4.0)时。我还得到了其他对象Id,如(7.0)和对象Id(6.0),等等。。。在
我的代码是:
from PyPDF2 import PdfFileReader
from pathlib import Path
import os
import math
import logging
numPages=0
workPath = input ('Please introduce your working directory: ')
print ('Your selected path is ' + workPath)
os.chdir (workPath.encode())
logging.basicConfig(filename='errrors.log',
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
fout= open('PagesCount.txt', 'w', encoding="utf-8")
path_files = Path(workPath)
for file in path_files.glob('**/*.pdf'):
page_Count = 0
try:
with open(str(file), "br") as PDF:
try:
page_Count = PdfFileReader(PDF).getNumPages()
numPages = numPages + page_Count
print ('Pages in ' + str(file) + ': ' + str(page_Count) + ' pages')
fout.write ('Pages in ' + str(file) + ':\t' + str(page_Count) + ' pages\n')
except:
print('File {} cannot be read'.format(str(file)))
logging.error('File cannot be read:\t {}'.format(str(file)))
except:
logging.error('File is not processed: {}'.format(str(file)))
print ('Total number of pages:\t' + str(numPages) + ' pages')
fout.write ('Total number of pages:\t' + str(numPages) + ' pages\n')
因此,碰巧我不得不计算许多扫描的PDF文件的页数,其中很高的一部分,大约80%的被过滤到错误文件中,因为上面提到的错误。有没有办法防止外部参照表非零索引错误?在
谢谢。在
我已经解决了部分问题。将参数strict设置为false,这样可以打开比以前更多的文件
从以下位置更改此行: page\u Count=PdfFileReader(PDF).getNumPages() 到 page\u Count=PdfFileReader(PDF,strict=False).getNumPages()
相关问题 更多 >
编程相关推荐