-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from Prem-Kumar-Dev/prem-kumar-dev
Added PDF Merger python app as permitted in PR #277.
- Loading branch information
Showing
7 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
Here is the `README.md` text specifically for the `pdf_merger` folder: | ||
|
||
```markdown | ||
|
||
## List of Scripts | ||
|
||
1. [PDF Merger](./pdf_merger/README.md) - A Python script that merges multiple PDF files into one. Useful for combining documents easily. | ||
|
||
|
||
# PDF Merger | ||
|
||
## Overview | ||
|
||
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. | ||
|
||
## How to Use | ||
|
||
1. Clone the repository or download the script. | ||
```bash | ||
git clone https://github.com/yourusername/python-scripts-repo | ||
``` | ||
|
||
2. Navigate to the `pdf_merger` folder. | ||
```bash | ||
cd pdf_merger | ||
``` | ||
|
||
3. Install the required dependencies: | ||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
4. Run the script: | ||
```bash | ||
python pdf_merger.py | ||
``` | ||
|
||
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. | ||
|
||
6. Specify the output directory and output filename for the merged PDF. The merged file will be saved at the location you specify. | ||
|
||
## Example | ||
|
||
```bash | ||
Enter the path of a PDF file to merge (or type 'done' to finish): /path/to/file1.pdf | ||
Enter the path of a PDF file to merge (or type 'done' to finish): /path/to/file2.pdf | ||
Enter the path of a PDF file to merge (or type 'done' to finish): done | ||
Enter the output directory (leave empty for current directory): /path/to/output/ | ||
Enter the output filename (e.g., merged.pdf): merged.pdf | ||
``` | ||
|
||
The merged PDF will be saved as `/path/to/output/merged.pdf`. | ||
|
||
## Prerequisites | ||
|
||
- Python 3.x | ||
- `PyPDF2` library | ||
|
||
Install the prerequisites by running: | ||
```bash | ||
pip install PyPDF2 | ||
``` | ||
|
||
## Additional Features | ||
|
||
- **Custom PDF order**: The script allows you to specify the order of the PDF files to merge. | ||
- **Error Handling**: Handles invalid file inputs and provides meaningful error messages. | ||
- **Output location**: You can specify the directory and filename for the merged PDF output. | ||
|
||
## Contribution | ||
|
||
Feel free to contribute by creating an issue and submitting a pull request. Ensure to update this README with any additional features or changes. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License. See the `LICENSE` file for more information. | ||
``` | ||
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! |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from pdf_merger.cli import main | ||
|
||
if __name__ == '__main__': | ||
main() |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from pdf_merger.merger import PDFMerger | ||
import os | ||
|
||
def get_pdf_order(pdf_list): | ||
"""Prompt the user to specify the order of PDFs to merge.""" | ||
print("\nCurrent PDF files:") | ||
for idx, pdf in enumerate(pdf_list): | ||
print(f"{idx + 1}: {pdf}") | ||
|
||
order_input = input("Enter the numbers of the PDFs in the desired order (comma-separated, e.g., 1,3,2): ") | ||
|
||
try: | ||
order = [int(num) - 1 for num in order_input.split(',')] | ||
ordered_pdfs = [pdf_list[i] for i in order if i < len(pdf_list)] | ||
return ordered_pdfs | ||
except ValueError: | ||
print("Error: Invalid input. Please enter numbers only.") | ||
return [] | ||
|
||
def main(): | ||
print("Welcome to the PDF Merger!") | ||
|
||
merger = PDFMerger() | ||
|
||
while True: | ||
pdf_file = input("Enter the path of a PDF file to merge (or type 'done' to finish): ") | ||
if pdf_file.lower() == 'done': | ||
break | ||
merger.add_pdf(pdf_file) | ||
|
||
if not merger.pdf_list: | ||
print("No valid PDF files to merge. Exiting...") | ||
return | ||
|
||
output_path = input("Enter the output directory (leave empty for current directory): ") | ||
output_filename = input("Enter the output filename (e.g., merged.pdf): ") | ||
|
||
if not output_filename.endswith('.pdf'): | ||
output_filename += '.pdf' | ||
|
||
if output_path: | ||
output_filename = os.path.join(output_path, output_filename) | ||
|
||
ordered_pdfs = get_pdf_order(merger.pdf_list) | ||
|
||
if not ordered_pdfs: | ||
print("No valid order provided. Exiting...") | ||
return | ||
|
||
merger.pdf_list = ordered_pdfs | ||
merger.merge_pdfs(output_filename) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import PyPDF2 | ||
import os | ||
|
||
class PDFMerger: | ||
def __init__(self): | ||
self.pdf_list = [] | ||
|
||
def add_pdf(self, pdf_file): | ||
|
||
if os.path.isfile(pdf_file) and pdf_file.endswith('.pdf'): | ||
self.pdf_list.append(pdf_file) | ||
print(f'Added: {pdf_file}') | ||
else: | ||
print(f'Error: Invalid file - {pdf_file}') | ||
|
||
def merge_pdfs(self, output_filename): | ||
|
||
pdf_writer = PyPDF2.PdfWriter() | ||
|
||
try: | ||
for pdf in self.pdf_list: | ||
pdf_reader = PyPDF2.PdfReader(pdf) | ||
for page in range(len(pdf_reader.pages)): | ||
pdf_writer.add_page(pdf_reader.pages[page]) | ||
|
||
with open(output_filename, 'wb') as output_pdf: | ||
pdf_writer.write(output_pdf) | ||
|
||
print(f'Merged {len(self.pdf_list)} PDFs into "{output_filename}".') | ||
except Exception as e: | ||
print(f'Error during merging: {e}') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PyPDF2 |
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