-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_merger_2.py
More file actions
76 lines (63 loc) · 2.79 KB
/
pdf_merger_2.py
File metadata and controls
76 lines (63 loc) · 2.79 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
# pdf_merger_2.py
import os
from PyPDF2 import PdfMerger, PdfReader
def merge_pdfs_in_directory(input_folder, output_folder, output_filename="merged_output.pdf"):
"""
Recursively finds all PDF files in the input_folder and merges them
into a single PDF file in the output_folder.
Args:
input_folder (str): The path to the folder containing PDF files.
output_folder (str): The path to the folder where the merged PDF will be saved.
output_filename (str): The name of the merged PDF file.
"""
if not os.path.exists(input_folder):
print(f"Error: Input folder '{input_folder}' does not exist.")
return
# --- This is the part that creates the output directory if it doesn't exist ---
if not os.path.exists(output_folder):
os.makedirs(output_folder)
print(f"Created output folder: '{output_folder}'")
# -----------------------------------------------------------------------------
merger = PdfMerger()
pdf_files_found = []
# Walk through the input folder recursively
for root, _, files in os.walk(input_folder):
for file in files:
if file.lower().endswith('.pdf'):
filepath = os.path.join(root, file)
pdf_files_found.append(filepath)
if not pdf_files_found:
print(f"No PDF files found in '{input_folder}' and its subdirectories.")
return
# Sort the files for consistent merging order (optional, but good practice)
pdf_files_found.sort()
print(f"Found {len(pdf_files_found)} PDF files to merge:")
for pdf_file in pdf_files_found:
print(f"- {pdf_file}")
try:
with open(pdf_file, 'rb') as f:
merger.append(PdfReader(f))
except Exception as e:
print(f"Error appending {pdf_file}: {e}")
output_filepath = os.path.join(output_folder, output_filename)
try:
with open(output_filepath, 'wb') as output_file:
merger.write(output_file)
print(f"\nSuccessfully merged PDF files to: '{output_filepath}'")
except Exception as e:
print(f"Error writing merged file: {e}")
finally:
merger.close()
if __name__ == "__main__":
script_dir = os.path.dirname(os.path.abspath(__file__))
input_folder_name = "input"
output_folder_name = "output"
# Construct full paths
input_path = os.path.join(script_dir, input_folder_name)
output_path = os.path.join(script_dir, output_folder_name)
# Example Usage:
# 1. Create an 'input' folder in the same directory as this script.
# 2. Place some PDF files (and even subfolders with PDFs) inside the 'input' folder.
# 3. Run this script.
# The merged PDF will be created in the 'output' folder.
merge_pdfs_in_directory(input_path, output_path, "all_merged_documents.pdf")