Spaces:
Paused
Paused
| import gradio as gr | |
| import fitz | |
| import os | |
| def append_suffix_to_filename(filepath, suffix): | |
| directory, filename = os.path.split(filepath) | |
| filename_without_extension, extension = os.path.splitext(filename) | |
| return os.path.join(directory, filename_without_extension + suffix + extension) | |
| def remove_password(input_files, password): | |
| output_files = [] | |
| for file in input_files: | |
| output_path = append_suffix_to_filename(file.name, '_open') | |
| try: | |
| doc = fitz.open(file.name) | |
| if doc.is_encrypted: | |
| doc.authenticate(password) | |
| doc.save(output_path) | |
| print(output_path) | |
| doc.close() | |
| output_files.append(output_path) | |
| except Exception as e: | |
| print(e) | |
| pass | |
| return output_files | |
| def pdf_password_remover(files, password): | |
| output_files = [] | |
| for file in files: | |
| output_files.extend(remove_password([file], password)) | |
| return output_files | |
| input_components = [ | |
| gr.Files(label="Upload PDF(s)"), | |
| gr.Textbox(label="Password", type="text") | |
| ] | |
| output_component = [gr.Files(label="Download Unlocked PDF(s)")] | |
| gr.Interface( | |
| fn=pdf_password_remover, | |
| inputs=input_components, | |
| outputs=output_component, | |
| title="PDF Password Remover", | |
| description="Upload password-protected PDF files and input a single password to unlock them." | |
| ).launch(debug=True) | |