Python3检验pdf文件是否有效

【基本原理】

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

【情形一】

  pdf文件在磁盘上。    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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的参数为文件名或文件对象,所以需要做一下转换。

方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

方法二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import  io, traceback
from  PyPDF2  import  PdfFileReader    
 
#参数为bytes类型数据。利用BytesIO转换。
def  isValidPDF_bytes(pdfBytes):
     bValid  =  True
     try :
         =  io.BytesIO(pdfBytes)
         reader  =  PdfFileReader(b)
         if  reader.getNumPages() <  1 :     #进一步通过页数判断。
             bValid  =  False
     except :
         bValid  =  False
         print ( '*'  +  traceback.format_exc())
         
     return  bValid

还可以利用PDFlib判断:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import  os
from  PDFlib.PDFlib  import  PDFlib
from  PDFlib.PDFlib  import  PDFlibException
 
def  isValidPdf(pathfile): 
     =  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


参考文档:

1、The PdfFileReader Class

2、tempfile — Generate temporary files and directories

3、io — Core tools for working with streams


*** walker ***

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1683680如需转载请自行联系原作者


RQSLT

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值