Skip to content

Commit b6755a4

Browse files
Merge pull request #2576 from tayyab-ilyas/pdf-merger
PDF Merger Script
2 parents 84c6b70 + 38c5a8e commit b6755a4

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

PDF_Merger/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# PDF Merger
2+
3+
A Python script that allows users to merge two PDF files into a single consolidated PDF document. It offers a user-friendly command-line interface, making PDF merging tasks quick and efficient.
4+
5+
## Description
6+
7+
nput files, ensuring that both files are accessible. If either or both files are not found, the script provides a warning message and proceeds to merge the available files.
8+
9+
Upon successfully merging the PDFs, the script prompts the user to specify the desired name for the output merged PDF file. The merged PDF is then created, combining the pages from the input PDFs in the order they were entered.
10+
11+
The script utilizes the PyPDF2 library to perform the PDF merging, ensuring compatibility with various PDF versions. Additionally, it includes basic error handling to notify the user of any issues with the input files.
12+
13+
## Usage
14+
15+
1. Make sure you have Python and the PyPDF2 library installed.
16+
2. Run the script and follow the prompts to enter the names of the two PDF files you want to merge.
17+
3. Specify the desired name for the output merged PDF file.
18+
4. The script will create the merged PDF with the specified name in the current directory.
19+
20+
## Requirements
21+
22+
- Python
23+
- PyPDF2 library
24+

PDF_Merger/pdfmerger.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import PyPDF2
2+
3+
def merge_pdfs(output_filename, input_files):
4+
pdf_merger = PyPDF2.PdfMerger()
5+
6+
for file in input_files:
7+
try:
8+
with open(file, 'rb') as pdf_file:
9+
pdf_merger.append(pdf_file)
10+
except FileNotFoundError:
11+
print(f"Warning: File '{file}' not found. Skipping...")
12+
13+
with open(output_filename, 'wb') as output_file:
14+
pdf_merger.write(output_file)
15+
16+
print(f"PDFs merged successfully! Output file: {output_filename}")
17+
18+
def main():
19+
print("PDF Merge Tool")
20+
print("Please enter the names of the two PDF files to merge:")
21+
22+
pdf1 = input("PDF 1: ")
23+
pdf2 = input("PDF 2: ")
24+
25+
output_filename = input("Enter the name for the merged PDF: ")
26+
27+
input_files = [pdf1, pdf2]
28+
29+
merge_pdfs(output_filename, input_files)
30+
31+
if __name__ == "__main__":
32+
main()

0 commit comments

Comments
 (0)