Skip to content

Commit cfbf90b

Browse files
Implemented PDF Merger and Splitter python script : Issue no DhanushNehru#283 (DhanushNehru#291)
* Create PDF Merger and Splitter.py * Update README.md * Create readme.md
1 parent 8690def commit cfbf90b

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from PyPDF2 import PdfReader, PdfWriter
2+
3+
def merge_pdfs(pdfs, output):
4+
pdf_writer = PdfWriter()
5+
for pdf in pdfs:
6+
reader = PdfReader(pdf)
7+
for page in range(len(reader.pages)):
8+
pdf_writer.add_page(reader.pages[page])
9+
10+
with open(output, 'wb') as out:
11+
pdf_writer.write(out)
12+
13+
def split_pdf(pdf):
14+
reader = PdfReader(pdf)
15+
for page_num, page in enumerate(reader.pages):
16+
writer = PdfWriter()
17+
writer.add_page(page)
18+
output_filename = f'{pdf.split(".")[0]}_page_{page_num+1}.pdf'
19+
with open(output_filename, 'wb') as out:
20+
writer.write(out)
21+
22+
merge_pdfs(['file1.pdf', 'file2.pdf'], 'merged.pdf')
23+
split_pdf('merged.pdf')

PDF Merger and Splitter/readme.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# PDF Merger and Splitter
2+
3+
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.
4+
5+
## Features
6+
7+
- **Merge PDFs**: Combine multiple PDF files into one.
8+
- **Split PDF**: Divide a single PDF file into separate pages, saving each page as a new PDF file.
9+
10+
## Requirements
11+
12+
Make sure you have Python installed on your machine. This script also requires the `PyPDF2` library. You can install it using pip:
13+
14+
```bash
15+
pip install PyPDF2
16+
```
17+
18+
# Usage
19+
## Merging PDFs
20+
- Place the PDF files you want to merge in the same directory as the script.
21+
- Modify the merge_pdfs function call in the script to include the names of the PDF files you want to merge. For example:
22+
```bash
23+
merge_pdfs(['file1.pdf', 'file2.pdf'], 'merged.pdf')
24+
```
25+
- Run the script:
26+
```bash
27+
python pdf_merger_splitter.py
28+
```
29+
## Splitting PDFs
30+
- 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.
31+
- You can also split any PDF file by calling the split_pdf function in the script:
32+
```bash
33+
split_pdf('your_pdf_file.pdf')
34+
```
35+
- Run the script to create separate PDF files for each page.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ More information on contributing and the general code of conduct for discussion
9090
| 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.|
9191
| PDF to Audio | [PDF to Audio](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20Audio) | Converts PDF to audio. |
9292
| PDF to Text | [PDF to text](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20text) | Converts PDF to text. |
93+
| 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.
9394
| Planet Simulation | [Planet Simulation](https://github.com/DhanushNehru/Python-Scripts/tree/master/Planet%20Simulation) | A simulation of several planets rotating around the sun.
9495
| 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.
9596
| Pigeonhole Sort | [Algorithm](https://github.com/DhanushNehru/Python-Scripts/tree/master/PigeonHole) | The pigeonhole sort algorithm to sort your arrays efficiently!

0 commit comments

Comments
 (0)