diff --git a/PDF Merger/README.md b/PDF Merger/README.md new file mode 100644 index 00000000..75003631 --- /dev/null +++ b/PDF Merger/README.md @@ -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! diff --git a/PDF Merger/pdf_merger.py b/PDF Merger/pdf_merger.py new file mode 100644 index 00000000..d71205d2 --- /dev/null +++ b/PDF Merger/pdf_merger.py @@ -0,0 +1,4 @@ +from pdf_merger.cli import main + +if __name__ == '__main__': + main() diff --git a/PDF Merger/pdf_merger/__init__.py b/PDF Merger/pdf_merger/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/PDF Merger/pdf_merger/cli.py b/PDF Merger/pdf_merger/cli.py new file mode 100644 index 00000000..6ea13abf --- /dev/null +++ b/PDF Merger/pdf_merger/cli.py @@ -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) diff --git a/PDF Merger/pdf_merger/merger.py b/PDF Merger/pdf_merger/merger.py new file mode 100644 index 00000000..3cdec3ea --- /dev/null +++ b/PDF Merger/pdf_merger/merger.py @@ -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}') diff --git a/PDF Merger/requirements.txt b/PDF Merger/requirements.txt new file mode 100644 index 00000000..e9a5c8ea --- /dev/null +++ b/PDF Merger/requirements.txt @@ -0,0 +1 @@ +PyPDF2 diff --git a/README.md b/README.md index b5090ad9..b22404fd 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ More information on contributing and the general code of conduct for discussion | OTP Verification | [OTP Verification](https://github.com/DhanushNehru/Python-Scripts/tree/master/OTP%20%20Verify) | An OTP Verification Checker. | | Password Generator | [Password Generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Password%20Generator) | Generates a random password. | | Password Manager | [Password Manager](https://github.com/nem5345/Python-Scripts/tree/master/Password%20Manager) | Generate and interact with a password manager. | +| 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. | | Planet Simulation | [Planet Simulation](https://github.com/DhanushNehru/Python-Scripts/tree/master/Planet%20Simulation) | A simulation of several planets rotating around the sun.