File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ import PyPDF2
2
+ import os
3
+
4
+ def protect_pdf (input_path , output_path , password ):
5
+ with open (input_path , 'rb' ) as file :
6
+ pdf_reader = PyPDF2 .PdfReader (file )
7
+ pdf_writer = PyPDF2 .PdfWriter ()
8
+
9
+ for page_num in range (len (pdf_reader .pages )):
10
+ pdf_writer .add_page (pdf_reader .pages [page_num ])
11
+
12
+ pdf_writer .encrypt (password )
13
+
14
+ output_folder = os .path .dirname (output_path )
15
+ os .makedirs (output_folder , exist_ok = True )
16
+
17
+ with open (output_path , 'wb' ) as output_file :
18
+ pdf_writer .write (output_file )
19
+
20
+ if __name__ == "__main__" :
21
+ input_pdf_path = "pdf-file/logo.pdf"
22
+ output_pdf_path = "output/protected_logo.pdf"
23
+ password = "123456789"
24
+
25
+ protect_pdf (input_pdf_path , output_pdf_path , password )
26
+ print (f"PDF file protected and saved to: { output_pdf_path } " )
You can’t perform that action at this time.
0 commit comments