4
4
import os
5
5
6
6
def add_frame (input_pdf_path , left = 20 , right = 20 , top = 20 , bottom = 20 , thickness = 2 ):
7
+
8
+
7
9
try :
8
10
doc = fitz .open (input_pdf_path )
9
-
11
+
10
12
for page_num in range (len (doc )):
11
13
page = doc [page_num ]
12
14
page_rect = page .rect
@@ -17,21 +19,21 @@ def add_frame(input_pdf_path, left=20, right=20, top=20, bottom=20, thickness=2)
17
19
page_rect .width - right , # right
18
20
page_rect .height - bottom # bottom
19
21
)
20
-
22
+
21
23
page .draw_rect (
22
24
frame_rect , # rectangle coordinates
23
25
width = thickness # frame thickness
24
26
)
25
-
27
+
26
28
# Set output filename if not provided
27
29
28
30
base , ext = os .path .splitext (input_pdf_path )
29
31
output_pdf_path = f"{ base } _framed.pdf"
30
-
32
+
31
33
doc .save (output_pdf_path )
32
34
print (f"PDF with rectangle frame saved to { output_pdf_path } " )
33
-
34
- except UnicodeDecodeError as e :
35
+
36
+ except UnicodeDecodeError :
35
37
print ("Error: Input file path encoding issue. Please ensure the file path is UTF-8 encoded." )
36
38
except Exception as e :
37
39
print (f"An error occurred: { e } " )
@@ -40,6 +42,8 @@ def add_frame(input_pdf_path, left=20, right=20, top=20, bottom=20, thickness=2)
40
42
doc .close ()
41
43
42
44
if __name__ == "__main__" :
45
+
46
+
43
47
parser = argparse .ArgumentParser (
44
48
description = "Add a rectangle frame to each page of a PDF document.\n "
45
49
"Flags: --l (left), --r (right), --t (top), --b (bottom), --th (thickness)" ,
@@ -64,4 +68,4 @@ def add_frame(input_pdf_path, left=20, right=20, top=20, bottom=20, thickness=2)
64
68
except Exception as e :
65
69
print (f"Error: { e } \n " )
66
70
parser .print_usage ()
67
- sys .exit (1 )
71
+ sys .exit (1 )
0 commit comments