From e4aa4ed38cf5a32137f23f00c4698261c7046a40 Mon Sep 17 00:00:00 2001 From: Prathamesh Ghatole Date: Mon, 9 Oct 2023 02:06:09 +0530 Subject: [PATCH] Added new script 'PDFsplitter' --- PDFsplitter/PDFsplitter.py | 44 ++++++++++++++++++++++++++++++++++++ PDFsplitter/README.md | 22 ++++++++++++++++++ PDFsplitter/requirements.txt | 1 + README.md | 16 +++++++------ 4 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 PDFsplitter/PDFsplitter.py create mode 100644 PDFsplitter/README.md create mode 100644 PDFsplitter/requirements.txt diff --git a/PDFsplitter/PDFsplitter.py b/PDFsplitter/PDFsplitter.py new file mode 100644 index 0000000..1a4ccd1 --- /dev/null +++ b/PDFsplitter/PDFsplitter.py @@ -0,0 +1,44 @@ +import PyPDF2 +import argparse +import os + + +def split_pdf(input_pdf_path, output_folder): + # Open the PDF file + pdf_file = open(input_pdf_path, "rb") + input_pdf_name = os.path.basename(input_pdf_path).split(".")[0] + pdf_reader = PyPDF2.PdfReader(pdf_file) + + # Create the output folder if it doesn't exist + os.makedirs(output_folder, exist_ok=True) + + # Loop through each page and save it as a separate PDF file + for page_num in range(len(pdf_reader.pages)): + pdf_writer = PyPDF2.PdfWriter() + pdf_writer.add_page(pdf_reader.pages[page_num]) + + output_pdf_path = os.path.join( + output_folder, f"{input_pdf_name}_{page_num + 1}.pdf" + ) + + with open(output_pdf_path, "wb") as output_pdf: + pdf_writer.write(output_pdf) + print(f"Page {page_num + 1} saved as {output_pdf_path}") + + # Close the input PDF file + pdf_file.close() + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Split a PDF file into separate pages") + parser.add_argument( + "input_pdf", help="Input PDF file path") + parser.add_argument( + "output_folder", help="Output folder path for split pages") + args = parser.parse_args() + + input_pdf_path = args.input_pdf + output_folder = args.output_folder + + split_pdf(input_pdf_path, output_folder) diff --git a/PDFsplitter/README.md b/PDFsplitter/README.md new file mode 100644 index 0000000..4359f18 --- /dev/null +++ b/PDFsplitter/README.md @@ -0,0 +1,22 @@ +## PDFsplitter + +This Python script allows you to split a PDF file into separate PDF files, one for each page. It uses the PyPDF2 library to perform the splitting. + +### Usage + +1. Make sure you have Python 3.x installed on your system. + +2. Install the required PyPDF2 library using pip: +```pip install PyPDF2``` + +3. Run the script with the following command: + + `python PDFsplitter.py input_pdf output_folder` +- `input_pdf`: The path to the input PDF file that you want to split. +- `output_folder`: The folder where the split PDF pages will be saved. + +### Example + +To split an input PDF file named `input.pdf` into separate pages and save them in an `output_pages` folder, you can run the following command: + + python PDFsplitter.py input.pdf output_pages \ No newline at end of file diff --git a/PDFsplitter/requirements.txt b/PDFsplitter/requirements.txt new file mode 100644 index 0000000..1bf0c7e --- /dev/null +++ b/PDFsplitter/requirements.txt @@ -0,0 +1 @@ +PyPDF2==3.0.1 diff --git a/README.md b/README.md index d8734e3..d97df7a 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,14 @@ ## Contents: -- [What is this repo?](#what-is-this-repo) -- [What do we have?](#what-do-we-have) -- [How to use?](#how-to-use) -- [Contribution Guidelines](#contributions-guidelines) - - [Steps required to follow before adding any script](#steps-required-to-follow-before-adding-any-script) -- [Contribution Guidelines](#contributions-guidelines) -- [If you like the project](#if-you-like-the-project) +- [Awesome Python Scripts :sunglasses: ](#awesome-python-scripts-sunglasses----) + - [Contents:](#contents) + - [What is this repo?](#what-is-this-repo) + - [What do we have:](#what-do-we-have) + - [How to use:](#how-to-use) + - [Contribution Guidelines:](#contribution-guidelines) + - [Steps required to follow before adding any script](#steps-required-to-follow-before-adding-any-script) +- [If you like the project:](#if-you-like-the-project) - [Want to connect with me?](#want-to-connect-with-me) ## What is this repo? @@ -96,6 +97,7 @@ So far, the following projects have been integrated to this repo: |[Minecraft Server in background](Minecraft_server_in_background)|[Max von Forell](https://github.com/mvforell)| |[Own IP locator](Location_Of_Own_IP_Adress)|[Chris]()| |[PDF2text](PDF2text)|[QuangPH](https://github.com/quangph-1686a)| +|[PDFsplitter](PDFsplitter)|[Prathamesh-Ghatole](https://github.com/Prathamesh-Ghatole)| |[PX to REM](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/PX-to-REM)|[Atthaphon Urairat](https://github.com/uatthaphon) | |[Pdf to AudioBook Converter](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/PdfToAudio)|[Ayesha Gull](https://github.com/ayeshag7/)| |[Plagiarism_detector](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Plagiarism_detector)|[Akshita Singhal](https://github.com/akshitasinghal4444)|