44import os
55
66def add_frame (input_pdf_path , left = 20 , right = 20 , top = 20 , bottom = 20 , thickness = 2 ):
7+
8+
79 try :
810 doc = fitz .open (input_pdf_path )
9-
11+
1012 for page_num in range (len (doc )):
1113 page = doc [page_num ]
1214 page_rect = page .rect
@@ -17,21 +19,21 @@ def add_frame(input_pdf_path, left=20, right=20, top=20, bottom=20, thickness=2)
1719 page_rect .width - right , # right
1820 page_rect .height - bottom # bottom
1921 )
20-
22+
2123 page .draw_rect (
2224 frame_rect , # rectangle coordinates
2325 width = thickness # frame thickness
2426 )
25-
27+
2628 # Set output filename if not provided
2729
2830 base , ext = os .path .splitext (input_pdf_path )
2931 output_pdf_path = f"{ base } _framed.pdf"
30-
32+
3133 doc .save (output_pdf_path )
3234 print (f"PDF with rectangle frame saved to { output_pdf_path } " )
33-
34- except UnicodeDecodeError as e :
35+
36+ except UnicodeDecodeError :
3537 print ("Error: Input file path encoding issue. Please ensure the file path is UTF-8 encoded." )
3638 except Exception as e :
3739 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)
4042 doc .close ()
4143
4244if __name__ == "__main__" :
45+
46+
4347 parser = argparse .ArgumentParser (
4448 description = "Add a rectangle frame to each page of a PDF document.\n "
4549 "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)
6468 except Exception as e :
6569 print (f"Error: { e } \n " )
6670 parser .print_usage ()
67- sys .exit (1 )
71+ sys .exit (1 )
0 commit comments