Fix PyPDF2 library deprecation
Updated script to use PdfReader to read the PDF files and PdfWriter to create new PDF files for each split page.
This commit is contained in:
parent
f3cee999ea
commit
3856a2c905
|
@ -1,6 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
from PyPDF2 import PdfFileReader, PdfFileWriter
|
from PyPDF2 import PdfReader, PdfWriter
|
||||||
|
|
||||||
def split_pdf_pages(processed_folder):
|
def split_pdf_pages(processed_folder):
|
||||||
# Check if the processed folder exists in the current directory, if not, create it
|
# Check if the processed folder exists in the current directory, if not, create it
|
||||||
|
@ -12,15 +12,15 @@ def split_pdf_pages(processed_folder):
|
||||||
if filename.endswith('.pdf'):
|
if filename.endswith('.pdf'):
|
||||||
# Read the PDF file
|
# Read the PDF file
|
||||||
with open(filename, 'rb') as infile:
|
with open(filename, 'rb') as infile:
|
||||||
reader = PdfFileReader(infile)
|
reader = PdfReader(infile)
|
||||||
num_pages = reader.numPages
|
num_pages = len(reader.pages)
|
||||||
|
|
||||||
# Check if PDF has more than 1 page
|
# Check if PDF has more than 1 page
|
||||||
if num_pages > 1:
|
if num_pages > 1:
|
||||||
# Split each page into a separate PDF
|
# Split each page into a separate PDF
|
||||||
for i in range(num_pages):
|
for i in range(num_pages):
|
||||||
writer = PdfFileWriter()
|
writer = PdfWriter()
|
||||||
writer.addPage(reader.getPage(i))
|
writer.add_page(reader.pages[i])
|
||||||
|
|
||||||
# Generate new filename for the split PDF
|
# Generate new filename for the split PDF
|
||||||
split_filename = f"split_{i+1}_{filename}"
|
split_filename = f"split_{i+1}_{filename}"
|
||||||
|
|
Loading…
Reference in New Issue