Created
July 30, 2025 03:33
-
-
Save IUHHUI/d549991e6eab13a744218b3206f8ffc2 to your computer and use it in GitHub Desktop.
检查pdf完整性
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import PyPDF2 | |
| def check_pdf(file_path): | |
| try: | |
| with open(file_path, 'rb') as file: | |
| pdf_reader = PyPDF2.PdfReader(file) | |
| # 获取 PDF 的页数,尝试读取以确认文件是否损坏 | |
| num_pages = len(pdf_reader.pages) | |
| print(f"文件 '{file_path}' 是有效的 PDF,包含 {num_pages} 页。") | |
| return True | |
| except PyPDF2.errors.PdfReadError as e: # 修正了这里 | |
| print(f"文件 '{file_path}' 损坏或无法解析: {e}") | |
| return False | |
| except Exception as e: | |
| print(f"发生未知错误: {e}") | |
| return False | |
| # 替换为你的 PDF 文件路径 | |
| file_path = "sehk25072501311_c.pdf" | |
| check_pdf(file_path) | |
| file_path = "2025060900023.pdf" | |
| check_pdf(file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment