Skip to content

Instantly share code, notes, and snippets.

@me-suzy
Created June 18, 2025 04:59
Show Gist options
  • Save me-suzy/082a8e05605c7124452c57bcc7be7f66 to your computer and use it in GitHub Desktop.
Save me-suzy/082a8e05605c7124452c57bcc7be7f66 to your computer and use it in GitHub Desktop.
verifica pdf daca se deschide.py
import os
from PyPDF2 import PdfReader
# Specify the directory path
directory = r"g:\De pus pe FTP 2\Revista Magazin\- 1999\1986 (Anul 29, nr. 1-26)"
# List to store damaged files
damaged_files = []
# Iterate through all files in the directory
for filename in os.listdir(directory):
if filename.lower().endswith('.pdf'):
file_path = os.path.join(directory, filename)
try:
# Attempt to open and read the PDF
with open(file_path, 'rb') as file:
pdf = PdfReader(file)
# Check if the PDF has at least one page
if len(pdf.pages) == 0:
damaged_files.append(filename)
else:
# Additional check: try to access the first page
pdf.pages[0]
except Exception as e:
damaged_files.append(filename)
# Write damaged files to a log file
if damaged_files:
with open('damaged_pdfs.log', 'w') as log_file:
log_file.write("The following PDF files are damaged and could not be repaired:\n")
for file in damaged_files:
log_file.write(f"- {file}\n")
print("Damaged files have been logged to 'damaged_pdfs.log'.")
else:
print("No damaged PDF files found.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment