Created
March 7, 2023 15:19
-
-
Save ahmedkhemiri95/182daa64a717a6cfb08fc4e8826051c7 to your computer and use it in GitHub Desktop.
Split PDFs documents page by page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This script consist of: | |
* Collect Pdf Files from uploads folder | |
* Split Pdf Files page by page. | |
* Save Splitted pdf pages to output Folder. | |
""" | |
import os | |
from PyPDF2 import PdfFileReader, PdfFileWriter | |
def splitting(upload_folder, split_folder): | |
'''Do collect PDF files, split pages and save them | |
''' | |
entries = os.listdir(upload_folder) | |
path = os.path.abspath(split_folder) | |
for entry in entries: | |
uploaded_file = os.path.join(upload_folder, entry) | |
output_file_folder = os.path.join(path, entry) | |
if not os.path.isdir(output_file_folder): | |
os.mkdir(output_file_folder) | |
pdf = PdfFileReader(uploaded_file, strict=False) | |
for page in range(pdf.getNumPages()): | |
pdf_writer = PdfFileWriter() | |
pdf_writer.addPage(pdf.getPage(page)) | |
output_filename = \ | |
os.path.join(output_file_folder, f'{page+1}.pdf') | |
with open(output_filename, 'wb') as out: | |
pdf_writer.write(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment