From 97eb694ebc7d0fae8c17cfa958e8c03193b3a04a Mon Sep 17 00:00:00 2001 From: Abhijit Motekar <109235675+AbhijitMotekar99@users.noreply.github.com> Date: Thu, 3 Oct 2024 22:48:19 +0530 Subject: [PATCH 1/3] Create PDF Merger and Splitter.py --- .../PDF Merger and Splitter.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 PDF Merger and Splitter/PDF Merger and Splitter.py diff --git a/PDF Merger and Splitter/PDF Merger and Splitter.py b/PDF Merger and Splitter/PDF Merger and Splitter.py new file mode 100644 index 00000000..09e37467 --- /dev/null +++ b/PDF Merger and Splitter/PDF Merger and Splitter.py @@ -0,0 +1,23 @@ +from PyPDF2 import PdfReader, PdfWriter + +def merge_pdfs(pdfs, output): + pdf_writer = PdfWriter() + for pdf in pdfs: + reader = PdfReader(pdf) + for page in range(len(reader.pages)): + pdf_writer.add_page(reader.pages[page]) + + with open(output, 'wb') as out: + pdf_writer.write(out) + +def split_pdf(pdf): + reader = PdfReader(pdf) + for page_num, page in enumerate(reader.pages): + writer = PdfWriter() + writer.add_page(page) + output_filename = f'{pdf.split(".")[0]}_page_{page_num+1}.pdf' + with open(output_filename, 'wb') as out: + writer.write(out) + +merge_pdfs(['file1.pdf', 'file2.pdf'], 'merged.pdf') +split_pdf('merged.pdf') From 4429ceb541a40cc2a7be3a28fccaa8e7cb934108 Mon Sep 17 00:00:00 2001 From: Abhijit Motekar <109235675+AbhijitMotekar99@users.noreply.github.com> Date: Fri, 4 Oct 2024 23:11:32 +0530 Subject: [PATCH 2/3] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b22404fd..92e6cdcd 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ More information on contributing and the general code of conduct for discussion | PDF Merger | [PDF Merger](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20Merger) |Merges multiple PDF files into a single PDF, with options for output location and custom order.| | PDF to Audio | [PDF to Audio](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20Audio) | Converts PDF to audio. | | PDF to Text | [PDF to text](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20text) | Converts PDF to text. | +| PDF merger and splitter | [PDF Merger and Splitter](https://github.com/AbhijitMotekar99/Python-Scripts/blob/master/PDF%20Merger%20and%20Splitter/PDF%20Merger%20and%20Splitter.py) | Create a tool that can merge multiple PDF files into one or split a single PDF into separate pages. | Planet Simulation | [Planet Simulation](https://github.com/DhanushNehru/Python-Scripts/tree/master/Planet%20Simulation) | A simulation of several planets rotating around the sun. | Playlist Exchange | [Playlist Exchange](https://github.com/DhanushNehru/Python-Scripts/tree/master/Playlist%20Exchange) | A Python script to exchange songs and playlists between Spotify and Python. | PNG TO JPG CONVERTOR | [PNG-To-JPG](https://github.com/DhanushNehru/Python-Scripts/tree/master/PNG%20To%20JPG) | A PNG TO JPG IMAGE CONVERTOR. From 5d038e4a880ac2307cb09597966823ca7fa6c6bb Mon Sep 17 00:00:00 2001 From: Abhijit Motekar <109235675+AbhijitMotekar99@users.noreply.github.com> Date: Sat, 5 Oct 2024 01:19:37 +0530 Subject: [PATCH 3/3] Create readme.md --- PDF Merger and Splitter/readme.md | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 PDF Merger and Splitter/readme.md diff --git a/PDF Merger and Splitter/readme.md b/PDF Merger and Splitter/readme.md new file mode 100644 index 00000000..a0aa8fcf --- /dev/null +++ b/PDF Merger and Splitter/readme.md @@ -0,0 +1,35 @@ +# PDF Merger and Splitter + +This Python script allows you to merge multiple PDF files into a single PDF and split a PDF file into individual pages. It utilizes the `PyPDF2` library for handling PDF files. + +## Features + +- **Merge PDFs**: Combine multiple PDF files into one. +- **Split PDF**: Divide a single PDF file into separate pages, saving each page as a new PDF file. + +## Requirements + +Make sure you have Python installed on your machine. This script also requires the `PyPDF2` library. You can install it using pip: + +```bash +pip install PyPDF2 +``` + +# Usage +## Merging PDFs +- Place the PDF files you want to merge in the same directory as the script. +- Modify the merge_pdfs function call in the script to include the names of the PDF files you want to merge. For example: + ```bash + merge_pdfs(['file1.pdf', 'file2.pdf'], 'merged.pdf') + ``` +- Run the script: + ```bash + python pdf_merger_splitter.py + ``` +## Splitting PDFs +- After merging, the script automatically splits the merged PDF file (merged.pdf) into separate pages. Each page will be saved as a new PDF file named merged_page_X.pdf, where X is the page number. +- You can also split any PDF file by calling the split_pdf function in the script: + ```bash + split_pdf('your_pdf_file.pdf') + ``` +- Run the script to create separate PDF files for each page.