Repository navigation
Expand file tree
/
Copy pathpdf_manager.py
More file actions
205 lines (141 loc) · 4.06 KB
/
Copy pathpdf_manager.py
File metadata and controls
205 lines (141 loc) · 4.06 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
from pathlib import Path
from PyPDF2 import PdfReader, PdfWriter, PdfMerger
# ==========================================
# PDF Manager Configuration
# ==========================================
INPUT_FOLDER = Path("pdfs/input")
OUTPUT_FOLDER = Path("pdfs/output")
# ==========================================
# Create Required Folders
# ==========================================
def create_folders():
INPUT_FOLDER.mkdir(parents=True, exist_ok=True)
OUTPUT_FOLDER.mkdir(parents=True, exist_ok=True)
# ==========================================
# List PDF Files
# ==========================================
def list_pdfs():
create_folders()
pdf_files = list(INPUT_FOLDER.glob("*.pdf"))
if not pdf_files:
return []
return pdf_files
# ==========================================
# Get PDF Information
# ==========================================
def get_pdf_info(file_path):
try:
reader = PdfReader(file_path)
return {
"name": Path(file_path).name,
"pages": len(reader.pages),
"size": round(
os.path.getsize(file_path) / 1024,
2
)
}
except Exception as e:
return {
"error": str(e)
}
# ==========================================
# Merge PDFs
# ==========================================
def merge_pdfs(pdf_files, output_name):
create_folders()
merger = PdfMerger()
try:
for pdf in pdf_files:
merger.append(str(pdf))
output_path = OUTPUT_FOLDER / output_name
merger.write(str(output_path))
merger.close()
return output_path
except Exception as e:
merger.close()
raise Exception(
f"Failed to merge PDFs: {e}"
)
# ==========================================
# Split PDF
# ==========================================
def split_pdf(file_path):
create_folders()
reader = PdfReader(file_path)
pdf_name = Path(file_path).stem
split_folder = (
OUTPUT_FOLDER / f"{pdf_name}_pages"
)
split_folder.mkdir(
parents=True,
exist_ok=True
)
created_files = []
for page_number, page in enumerate(
reader.pages,
start=1
):
writer = PdfWriter()
writer.add_page(page)
output_path = (
split_folder
/ f"{pdf_name}_page_{page_number}.pdf"
)
with open(output_path, "wb") as output_file:
writer.write(output_file)
created_files.append(output_path)
return created_files
# ==========================================
# Extract Pages
# ==========================================
def extract_pages(
file_path,
start_page,
end_page,
output_name
):
create_folders()
reader = PdfReader(file_path)
writer = PdfWriter()
total_pages = len(reader.pages)
if start_page < 1 or end_page > total_pages:
raise ValueError(
f"Page range must be between "
f"1 and {total_pages}."
)
if start_page > end_page:
raise ValueError(
"Starting page cannot be greater "
"than ending page."
)
for page_number in range(
start_page - 1,
end_page
):
writer.add_page(
reader.pages[page_number]
)
output_path = OUTPUT_FOLDER / output_name
with open(output_path, "wb") as output_file:
writer.write(output_file)
return output_path
# ==========================================
# Rename PDF
# ==========================================
def rename_pdf(file_path, new_name):
file_path = Path(file_path)
if not new_name.lower().endswith(".pdf"):
new_name += ".pdf"
new_path = file_path.parent / new_name
file_path.rename(new_path)
return new_path
# ==========================================
# Delete PDF
# ==========================================
def delete_pdf(file_path):
file_path = Path(file_path)
if file_path.exists():
file_path.unlink()
return True
return False