python批量判断pdf文件是否损坏

import PyPDF2


def is_pdf_corrupted(pdf_path):
    try:
        with open(pdf_path, 'rb') as file:
            reader = PyPDF2.PdfReader(file)
            # 尝试读取第一页,如果PDF损坏,这里会抛出异常
            firstpage=reader.pages[0]
            return False  # 如果没有异常,文件应该不是损坏的
    except Exception as e:
        print(f"PDF文件损坏: {e}")
        return True  # 如果有异常,文件可能是损坏的


#  folder_root  pdf 文件夹路径
def pdf_check(folder_root):
    for each_pdf in Path(folder_root).files('*.pdf'):
        is_corrupted = is_pdf_corrupted(each_pdf)
        if is_corrupted :
            print('{} is error'.format(each_pdf))
        else:
            print('{} is good'.format(each_pdf))

你可能感兴趣的:(python,pdf,开发语言)