Spaces:
Sleeping
Sleeping
File size: 1,233 Bytes
80a3675 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #!/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()
|