-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDF-merger.py
More file actions
102 lines (84 loc) · 3.01 KB
/
PDF-merger.py
File metadata and controls
102 lines (84 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
PDF Merger Utility (Production Ready)
Description: Merges multiple PDFs from different directories into one file.
Author: Rizwan Ahmed (RZ-Logic)
"""
import pypdf
import sys
import shlex
import warnings
from pathlib import Path
# Suppress specific PyPDF warnings
warnings.filterwarnings("ignore", category=UserWarning, module="pypdf")
def pdf_combiner(pdf_list, output_filename='super.pdf'):
merger = pypdf.PdfWriter()
try:
for pdf in pdf_list:
print(f"Adding: {pdf.name}")
merger.append(pdf)
output_path = Path.cwd() / output_filename
with open(output_path, 'wb') as out:
merger.write(out)
print(f"\n✅ Created: {output_path}")
except Exception as e:
print(f"\n❌ Error: {e}")
finally:
merger.close()
def show_examples():
example_text = """
💡 TIP: You can drag and drop files or folders directly into this terminal!
USAGE EXAMPLES:
1. Single File: "C:\\Docs\\Invoice.pdf"
2. Entire Folder: "C:\\Users\\HomePC\\Downloads"
3. Bulk Paste: "File1.pdf" "File2.pdf" "C:\\Invoices"
4. Finish: Type 'done' to merge everything.
5. Cancel: Type 'quit' or 'exit' to close without merging.
"""
print(example_text)
def main():
print("--- 📄 Professional PDF Merger ---")
show_examples()
pdf_files = []
while True:
user_input = input("Enter path(s), 'done', or 'quit': ").strip()
# Check for exit commands first
if user_input.lower() in ['quit', 'exit']:
print("\n👋 Exiting program. No files were merged.")
return # Exits the main function immediately
if user_input.lower() == 'done':
break
if not user_input:
continue
try:
paths = shlex.split(user_input)
except ValueError:
print("❌ Formatting error. Check your quotes!")
continue
for p in paths:
path = Path(p.strip('"\'')).resolve()
if path.is_file():
if path.suffix.lower() == '.pdf':
pdf_files.append(path)
print(f" + Added File: {path.name}")
else:
print(f" x Ignored: {path.name} (Not a PDF)")
elif path.is_dir():
found = sorted(path.glob("*.pdf"))
if found:
pdf_files.extend(found)
print(f" + Added {len(found)} PDFs from folder: {path.name}")
else:
print(f" ⚠️ No PDFs found in: {path.name}")
else:
print(f" ❌ Path not found: {p}")
if pdf_files:
print(f"\n⏳ Merging {len(pdf_files)} total files...")
pdf_combiner(pdf_files)
else:
print("\n⚠️ No files selected. Exiting.")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n🛑 Process stopped by user.")
sys.exit(0)