nrotem commited on
Commit
ca8d0bb
·
verified ·
1 Parent(s): 790fa0f

first commit

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py CHANGED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import fitz
3
+ import os
4
+
5
+
6
+ def append_suffix_to_filename(filepath, suffix):
7
+ directory, filename = os.path.split(filepath)
8
+ filename_without_extension, extension = os.path.splitext(filename)
9
+ return os.path.join(directory, filename_without_extension + suffix + extension)
10
+
11
+
12
+ def remove_password(input_files, password):
13
+ output_files = []
14
+ for file in input_files:
15
+ output_path = append_suffix_to_filename(file.name, '_open')
16
+ try:
17
+ doc = fitz.open(file.name)
18
+ if doc.is_encrypted:
19
+ doc.authenticate(password)
20
+ doc.save(output_path)
21
+ print(output_path)
22
+ doc.close()
23
+ output_files.append(output_path)
24
+ except Exception as e:
25
+ print(e)
26
+ pass
27
+ return output_files
28
+
29
+
30
+ def pdf_password_remover(files, password):
31
+ output_files = []
32
+ for file in files:
33
+ output_files.extend(remove_password([file], password))
34
+ return output_files
35
+
36
+
37
+ input_components = [
38
+ gr.Files(label="Upload PDF(s)"),
39
+ gr.Textbox(label="Password", type="text")
40
+ ]
41
+
42
+ output_component = [gr.Files(label="Download Unlocked PDF(s)")]
43
+
44
+ gr.Interface(
45
+ fn=pdf_password_remover,
46
+ inputs=input_components,
47
+ outputs=output_component,
48
+ title="PDF Password Remover",
49
+ description="Upload password-protected PDF files and input a single password to unlock them."
50
+ ).launch(debug=True)