forked from marlanperumal/pdf_statement_reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.py
More file actions
35 lines (27 loc) · 1.14 KB
/
Copy pathdecrypt.py
File metadata and controls
35 lines (27 loc) · 1.14 KB
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
from pikepdf import Pdf
import click
def decrypt_pdf(input_filename, output_filename=None, password=None):
"""Decrypts a pdf file
Uses pikepdf to open an encrypted pdf file and then save the unencrypted version.
If no output_filename is specified then overwrites the original file.
If no password is supplied then checks if file is already unencrypted
Args:
input_filename: The source encrypted pdf file
output_filename: The filename to write the decrypted file to
password: The password on the encrypted pdf file
Returns:
True if decryption succeeded, False if the file was already unencrypted.
If decryption fails, an error is raised
Raises:
FileNotFoundError: if output cannot be written
PdfError: if the file is not a valid pdf
PasswordError: if incorrect password supplied
"""
if password is None:
pdf = Pdf.open(input_filename)
click.echo("{} was already unencrypted")
return False
if output_filename is None:
output_filename = input_filename
pdf = Pdf.open(input_filename, password)
pdf.save(output_filename)