Fixed pdf movement getting stuck

Fixed it so that with Open(file_path, 'rb') as infile block is fully exited before shutil.move is called. Exiting the with block ensures that infile is closed.
This commit is contained in:
Steve Dogiakos 2024-02-10 13:54:04 -07:00 committed by GitHub
parent 3856a2c905
commit 43e8fd329f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 17 deletions

View File

@ -3,38 +3,29 @@ import shutil
from PyPDF2 import PdfReader, PdfWriter
def split_pdf_pages(processed_folder):
# Check if the processed folder exists in the current directory, if not, create it
if not os.path.exists(processed_folder):
os.makedirs(processed_folder)
# Iterate over all PDF files in the current directory
for filename in os.listdir('.'):
if filename.endswith('.pdf'):
# Read the PDF file
with open(filename, 'rb') as infile:
file_path = os.path.join('.', filename) # Added for clarity
with open(file_path, 'rb') as infile:
reader = PdfReader(infile)
num_pages = len(reader.pages)
# Check if PDF has more than 1 page
if num_pages > 1:
# Split each page into a separate PDF
for i in range(num_pages):
writer = PdfWriter()
writer.add_page(reader.pages[i])
# Generate new filename for the split PDF
split_filename = f"split_{i+1}_{filename}"
split_filepath = os.path.join(processed_folder, split_filename)
# Write the split PDF to the processed folder
with open(split_filepath, 'wb') as outfile:
writer.write(outfile)
# Move the original PDF to the processed folder after splitting or if it's single-paged
shutil.move(filename, os.path.join(processed_folder, filename))
# Define the name of the processed folder
# Attempt to move the file after ensuring it's closed
shutil.move(file_path, os.path.join(processed_folder, filename))
processed_folder = 'Processed'
# Run the function
split_pdf_pages(processed_folder)