Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """Unlock (decrypt) a password-protected PDF using pikepdf.""" | |
| import argparse, json, sys | |
| from pathlib import Path | |
| def unlock(input_path, output_path, password): | |
| import pikepdf | |
| if not output_path: | |
| output_path = str(Path(input_path).with_stem(Path(input_path).stem + '_unlocked')) | |
| try: | |
| pdf = pikepdf.open(input_path, password=password) | |
| except pikepdf.PasswordError: | |
| raise ValueError("Incorrect password. Please check and try again.") | |
| pdf.save(output_path) | |
| pdf.close() | |
| return output_path | |
| def main(): | |
| parser = argparse.ArgumentParser(description='Unlock password-protected PDF') | |
| parser.add_argument('--input', required=True) | |
| parser.add_argument('--output', required=True) | |
| parser.add_argument('--password', required=True) | |
| args = parser.parse_args() | |
| try: | |
| result = unlock(args.input, args.output, args.password) | |
| print(json.dumps({"success": True, "output": result, "message": "PDF unlocked successfully"})) | |
| except Exception as e: | |
| print(json.dumps({"success": False, "output": "", "message": str(e)})) | |
| sys.exit(1) | |
| if __name__ == '__main__': | |
| main() | |