python 打开pdf文件_Python3检验pdf文件是否有效

【基本原理】

利用PyPDF2的PdfFileReader模块打开pdf文件,如果不抛异常,就认为此pdf文件有效。有时打开并不抛出异常,但是有这种警告:UserWarning: startxref on same line as offset [pdf.py:1680]。这种情况pdf多半也是坏的,可进一步通过页数判断。但walker在测试中发现,对于正常pdf文件,进一步通过页数判断时有时会抛出异常。

【情形一】

pdf文件在磁盘上。import traceback

from PyPDF2 import PdfFileReader

#参数为pdf文件全路径名

def isValidPDF_pathfile(pathfile):

bValid = True

try:

#PdfFileReader(open(pathfile, 'rb'))

reader = PdfFileReader(pathfile)

if reader.getNumPages() < 1: #进一步通过页数判断。

bValid = False

except:

bValid = False

print('*' + traceback.format_exc())

return bValid

【情形二】

pdf是来自网络的bytes数据。由于PdfFileReader的参数为文件名或文件对象,所以需要做一下转换。

方法一:import traceback, tempfile

from PyPDF2 import PdfFileReader

#参数为bytes类型数据。利用临时文件。

def isValidPDF_bytes(pdfBytes):

bValid = True

try:

fp = tempfile.TemporaryFile()

fp.write(pdfBytes)

reader = PdfFileReader(fp)

fp.close()

if reader.getNumPages() < 1: #进一步通过页数判断。

bValid = False

except:

bValid = False

print('*' + traceback.format_exc())

return bValid

方法二:import io, traceback

from PyPDF2 import PdfFileReader

#参数为bytes类型数据。利用BytesIO转换。

def isValidPDF_bytes(pdfBytes):

bValid = True

try:

b = io.BytesIO(pdfBytes)

reader = PdfFileReader(b)

if reader.getNumPages() < 1: #进一步通过页数判断。

bValid = False

except:

bValid = False

print('*' + traceback.format_exc())

return bValid

还可以利用PDFlib判断:import os

from PDFlib.PDFlib import PDFlib

from PDFlib.PDFlib import PDFlibException

def isValidPdf(pathfile):

p = PDFlib()

p.set_option("license=xxxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx")

p.set_option("errorpolicy=return");

indoc = p.open_pdi_document(pathfile, 'repair=none');

print('indoc:' + str(indoc))

print('pathfile size:' + str(os.path.getsize(pathfile)) + 'B')

bValid = False

if (indoc == -1):

print('*' + p.get_errmsg())

bValid = False

else:

pageNumber = p.pcos_get_number(indoc, "length:pages")

print('pageNumber:' + str(pageNumber))

if pageNumber < 1: #页数为0

bValid = False

else:

bValid = True

if bValid:

p.close_pdi_document(indoc)

return bValid

*** updated * 2018-12-12 ***# encoding: utf-8

# author: walker

# date: 2018-12-12

# summary: 直接用 PDF 文件内容判断 PDF 的正确性和完整性,适用于判断下载的 PDF

import re

def isValidPDF_pathfile(pathfile):

r"""

直接用文件内容判断头尾,

参数为pdf文件全路径名

"""

content = ''

with open(pathfile, mode='rb') as f:

content = f.read()

partBegin = content[0:20]

if partBegin.find(rb'%PDF-1.') < 0:

print('Error: not find %PDF-1.')

return False

idx = content.rfind(rb'%%EOF')

if idx < 0:

print('Error: not find %%EOF')

return False

partEnd = content[(0 if idx-100 < 0 else idx-100) : idx + 5]

if not re.search(rb'startxref\s+\d+\s+%%EOF$', partEnd):

print('Error: not find startxref')

return False

return True

【相关阅读】

*** walker ***

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值