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:
parent
3856a2c905
commit
43e8fd329f
|
@ -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))
|
||||
# Attempt to move the file after ensuring it's closed
|
||||
shutil.move(file_path, os.path.join(processed_folder, filename))
|
||||
|
||||
# Define the name of the processed folder
|
||||
processed_folder = 'Processed'
|
||||
|
||||
# Run the function
|
||||
split_pdf_pages(processed_folder)
|
||||
|
|
Loading…
Reference in New Issue