python 判断pdf文件是否可用

# -*- coding: utf-8 -*-
# @Time    : 2021/12/28 16:52
# @Author  : Cocktail_py
"""
pip3 install -i http://mirrors.aliyun.com/pypi/simple/  pikepdf --trusted-host mirrors.aliyun.com

pip3 install -i http://mirrors.aliyun.com/pypi/simple/  pypdf --trusted-host mirrors.aliyun.com

"""

import PyPDF2
import pypdf
import pikepdf
from traceback import format_exc

def check_if_pdf_valid(filename):
    """
    判断pdf文件是否可用
    :param filename:
    :return:
    """
    try:
        PyPDF2.PdfFileReader(open(filename, "rb"))
    except:
        try:
            pikepdf.Pdf.open(filename)
        except:
            try:
                pypdf.PdfReader(filename)
            except:
                print(format_exc())
                return False
            else:
                return True
        else:
            return True
    else:
        return True

参考:https://www.cnpython.com/qa/50966

你可能感兴趣的:(个人学习记录,python,开发语言,后端)