From 8b081988d9e90dcedf906810a4a83c4956f79adc Mon Sep 17 00:00:00 2001 From: Aniz <20am014@gmail.com> Date: Thu, 20 Oct 2022 23:50:55 +0530 Subject: [PATCH 1/3] Pdf merger added --- Python/PDF_Merger/pdf.py | 33 +++++++++++++++++++++++++++ Python/PDF_Merger/to_convert/note.txt | 1 + 2 files changed, 34 insertions(+) create mode 100644 Python/PDF_Merger/pdf.py create mode 100644 Python/PDF_Merger/to_convert/note.txt diff --git a/Python/PDF_Merger/pdf.py b/Python/PDF_Merger/pdf.py new file mode 100644 index 00000000..ce3cd7dd --- /dev/null +++ b/Python/PDF_Merger/pdf.py @@ -0,0 +1,33 @@ +from csv import writer +from pathlib import Path +from PyPDF2 import PdfFileMerger, PdfFileReader +import sys +import os + + +# Define input directory for the pdf files +pdf_dir = Path(__file__).parent / "to_convert" + +# The output directory. +pdf_output_dir = Path(__file__).parent / "converted" +pdf_output_dir.mkdir(parents=True, exist_ok=True) + +# To take the list of all the .pdf files +pdf_files = list(pdf_dir.glob("*.pdf")) + +# Use the first 3 characters as the 'key' +keys = set([file.name[:3] for file in pdf_files]) + +# Determine the file name length of the base file + +BASE_FILE_NAME_LENGTH = 20 + +for key in keys: + merger = PdfFileMerger() + for file in pdf_files: + if file.name.startswith(key): + merger.append(PdfFileReader(str(file), "rb")) + if len(file.name) >= BASE_FILE_NAME_LENGTH: + base_file_name = file.name + merger.write(str(pdf_output_dir / base_file_name)) + merger.close() \ No newline at end of file diff --git a/Python/PDF_Merger/to_convert/note.txt b/Python/PDF_Merger/to_convert/note.txt new file mode 100644 index 00000000..8451715c --- /dev/null +++ b/Python/PDF_Merger/to_convert/note.txt @@ -0,0 +1 @@ +Add the files you want to merge in this directory \ No newline at end of file From 242471ac9b545d39684056754b2fadfa8418283f Mon Sep 17 00:00:00 2001 From: Aniz <20am014@gmail.com> Date: Thu, 20 Oct 2022 23:53:29 +0530 Subject: [PATCH 2/3] adding note --- Python/PDF_Merger/converted/note.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Python/PDF_Merger/converted/note.txt diff --git a/Python/PDF_Merger/converted/note.txt b/Python/PDF_Merger/converted/note.txt new file mode 100644 index 00000000..af0e0da9 --- /dev/null +++ b/Python/PDF_Merger/converted/note.txt @@ -0,0 +1 @@ +The converted file will appear in this folder. \ No newline at end of file From 0027b40f4d52be16c4f05498fc7e568df9c6445c Mon Sep 17 00:00:00 2001 From: Aniz B N <91843271+4N1Z@users.noreply.github.com> Date: Fri, 21 Oct 2022 00:05:04 +0530 Subject: [PATCH 3/3] Create PDF_Merger.md --- Python/PDF_Merger/PDF_Merger.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Python/PDF_Merger/PDF_Merger.md diff --git a/Python/PDF_Merger/PDF_Merger.md b/Python/PDF_Merger/PDF_Merger.md new file mode 100644 index 00000000..bb62d153 --- /dev/null +++ b/Python/PDF_Merger/PDF_Merger.md @@ -0,0 +1,22 @@ +## PDF MERGER +- This is just a simple, yet useful code written in Python. +- Easy to use as well as really helpful +- Allows you to merge more than one PDF files into a single PDF. + +### Instruction +- First and foremost copy and paste the code somewhere in your computer or download the PDF_Merger file +- Add all the PDF you want to convert into the [to_convert](https://github.com/4N1Z/code-n-stitch/tree/master/Python/PDF_Merger/to_convert). +- Then run the code +- The merged PDF will appear at [Converted file](https://github.com/4N1Z/code-n-stitch/tree/master/Python/PDF_Merger/converted). + +## Requirements +- You should be able to run python inside your computer +> If not then refer [here](https://www.python.org/) +- Then go to your terminal and you should install PyPDF2, which is a python library. +> [Refer here](https://pypi.org/project/PyPDF2/) + +#### That's it , with that you musst be able to merge your PDF's. 😄 + + + +