Create pdf_splitter.py

First Commit
This commit is contained in:
Steve Dogiakos 2024-02-10 13:49:26 -07:00 committed by GitHub
parent b13fb7bb1a
commit f3cee999ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 40 additions and 0 deletions

40
pdf_splitter.py Normal file
View File

@ -0,0 +1,40 @@
import os
import shutil
from PyPDF2 import PdfFileReader, PdfFileWriter
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:
reader = PdfFileReader(infile)
num_pages = reader.numPages
# 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 = PdfFileWriter()
writer.addPage(reader.getPage(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
processed_folder = 'Processed'
# Run the function
split_pdf_pages(processed_folder)