Skip to content

Commit 5738550

Browse files
authored
Merge pull request #279 from Prem-Kumar-Dev/prem-kumar-dev
Added PDF Merger python app as permitted in PR #277.
2 parents 5a12f82 + 990fed6 commit 5738550

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
lines changed

PDF Merger/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Here is the `README.md` text specifically for the `pdf_merger` folder:
2+
3+
```markdown
4+
5+
## List of Scripts
6+
7+
1. [PDF Merger](./pdf_merger/README.md) - A Python script that merges multiple PDF files into one. Useful for combining documents easily.
8+
9+
10+
# PDF Merger
11+
12+
## Overview
13+
14+
The **PDF Merger** is a Python script that allows you to merge multiple PDF files into a single file. It uses the `PyPDF2` library to handle PDF manipulation and provides an easy-to-use command-line interface to input the desired PDF files and specify the output location.
15+
16+
## How to Use
17+
18+
1. Clone the repository or download the script.
19+
```bash
20+
git clone https://github.com/yourusername/python-scripts-repo
21+
```
22+
23+
2. Navigate to the `pdf_merger` folder.
24+
```bash
25+
cd pdf_merger
26+
```
27+
28+
3. Install the required dependencies:
29+
```bash
30+
pip install -r requirements.txt
31+
```
32+
33+
4. Run the script:
34+
```bash
35+
python pdf_merger.py
36+
```
37+
38+
5. The script will prompt you to enter the path to each PDF file you want to merge. After entering all PDF files, type `done` to proceed.
39+
40+
6. Specify the output directory and output filename for the merged PDF. The merged file will be saved at the location you specify.
41+
42+
## Example
43+
44+
```bash
45+
Enter the path of a PDF file to merge (or type 'done' to finish): /path/to/file1.pdf
46+
Enter the path of a PDF file to merge (or type 'done' to finish): /path/to/file2.pdf
47+
Enter the path of a PDF file to merge (or type 'done' to finish): done
48+
Enter the output directory (leave empty for current directory): /path/to/output/
49+
Enter the output filename (e.g., merged.pdf): merged.pdf
50+
```
51+
52+
The merged PDF will be saved as `/path/to/output/merged.pdf`.
53+
54+
## Prerequisites
55+
56+
- Python 3.x
57+
- `PyPDF2` library
58+
59+
Install the prerequisites by running:
60+
```bash
61+
pip install PyPDF2
62+
```
63+
64+
## Additional Features
65+
66+
- **Custom PDF order**: The script allows you to specify the order of the PDF files to merge.
67+
- **Error Handling**: Handles invalid file inputs and provides meaningful error messages.
68+
- **Output location**: You can specify the directory and filename for the merged PDF output.
69+
70+
## Contribution
71+
72+
Feel free to contribute by creating an issue and submitting a pull request. Ensure to update this README with any additional features or changes.
73+
74+
## License
75+
76+
This project is licensed under the MIT License. See the `LICENSE` file for more information.
77+
```
78+
79+
You can copy and paste this directly into the `README.md` file for the `pdf_merger` folder. Let me know if you need further changes!

PDF Merger/pdf_merger.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from pdf_merger.cli import main
2+
3+
if __name__ == '__main__':
4+
main()

PDF Merger/pdf_merger/__init__.py

Whitespace-only changes.

PDF Merger/pdf_merger/cli.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from pdf_merger.merger import PDFMerger
2+
import os
3+
4+
def get_pdf_order(pdf_list):
5+
"""Prompt the user to specify the order of PDFs to merge."""
6+
print("\nCurrent PDF files:")
7+
for idx, pdf in enumerate(pdf_list):
8+
print(f"{idx + 1}: {pdf}")
9+
10+
order_input = input("Enter the numbers of the PDFs in the desired order (comma-separated, e.g., 1,3,2): ")
11+
12+
try:
13+
order = [int(num) - 1 for num in order_input.split(',')]
14+
ordered_pdfs = [pdf_list[i] for i in order if i < len(pdf_list)]
15+
return ordered_pdfs
16+
except ValueError:
17+
print("Error: Invalid input. Please enter numbers only.")
18+
return []
19+
20+
def main():
21+
print("Welcome to the PDF Merger!")
22+
23+
merger = PDFMerger()
24+
25+
while True:
26+
pdf_file = input("Enter the path of a PDF file to merge (or type 'done' to finish): ")
27+
if pdf_file.lower() == 'done':
28+
break
29+
merger.add_pdf(pdf_file)
30+
31+
if not merger.pdf_list:
32+
print("No valid PDF files to merge. Exiting...")
33+
return
34+
35+
output_path = input("Enter the output directory (leave empty for current directory): ")
36+
output_filename = input("Enter the output filename (e.g., merged.pdf): ")
37+
38+
if not output_filename.endswith('.pdf'):
39+
output_filename += '.pdf'
40+
41+
if output_path:
42+
output_filename = os.path.join(output_path, output_filename)
43+
44+
ordered_pdfs = get_pdf_order(merger.pdf_list)
45+
46+
if not ordered_pdfs:
47+
print("No valid order provided. Exiting...")
48+
return
49+
50+
merger.pdf_list = ordered_pdfs
51+
merger.merge_pdfs(output_filename)

PDF Merger/pdf_merger/merger.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import PyPDF2
2+
import os
3+
4+
class PDFMerger:
5+
def __init__(self):
6+
self.pdf_list = []
7+
8+
def add_pdf(self, pdf_file):
9+
10+
if os.path.isfile(pdf_file) and pdf_file.endswith('.pdf'):
11+
self.pdf_list.append(pdf_file)
12+
print(f'Added: {pdf_file}')
13+
else:
14+
print(f'Error: Invalid file - {pdf_file}')
15+
16+
def merge_pdfs(self, output_filename):
17+
18+
pdf_writer = PyPDF2.PdfWriter()
19+
20+
try:
21+
for pdf in self.pdf_list:
22+
pdf_reader = PyPDF2.PdfReader(pdf)
23+
for page in range(len(pdf_reader.pages)):
24+
pdf_writer.add_page(pdf_reader.pages[page])
25+
26+
with open(output_filename, 'wb') as output_pdf:
27+
pdf_writer.write(output_pdf)
28+
29+
print(f'Merged {len(self.pdf_list)} PDFs into "{output_filename}".')
30+
except Exception as e:
31+
print(f'Error during merging: {e}')

PDF Merger/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PyPDF2

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ More information on contributing and the general code of conduct for discussion
8787
| OTP Verification | [OTP Verification](https://github.com/DhanushNehru/Python-Scripts/tree/master/OTP%20%20Verify) | An OTP Verification Checker. |
8888
| Password Generator | [Password Generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Password%20Generator) | Generates a random password. |
8989
| Password Manager | [Password Manager](https://github.com/nem5345/Python-Scripts/tree/master/Password%20Manager) | Generate and interact with a password manager. |
90+
| 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.|
9091
| PDF to Audio | [PDF to Audio](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20Audio) | Converts PDF to audio. |
9192
| PDF to Text | [PDF to text](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20text) | Converts PDF to text. |
9293
| Planet Simulation | [Planet Simulation](https://github.com/DhanushNehru/Python-Scripts/tree/master/Planet%20Simulation) | A simulation of several planets rotating around the sun.

0 commit comments

Comments
 (0)