From a00358182534a9fc88a332a68e160dc700e63d41 Mon Sep 17 00:00:00 2001 From: Dan Howe Date: Thu, 12 Apr 2018 10:04:28 +1000 Subject: [PATCH] Add error handling for invalid pdfs --- scripts/pdf_unlock.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/scripts/pdf_unlock.py b/scripts/pdf_unlock.py index dd2472d..ba7e29f 100644 --- a/scripts/pdf_unlock.py +++ b/scripts/pdf_unlock.py @@ -76,14 +76,21 @@ def isencrypted(pdf_name): Returns: True if encrypted, otherwise False + + Raises: + AttributeError if pdf info cannot be obtained """ - pdf_info = pdfinfo(pdf_name) - status = pdf_info['Encrypted'].split(' ')[0] - - if status == 'yes': - return True - else: - return False + try: + pdf_info = pdfinfo(pdf_name) + status = pdf_info['Encrypted'].split(' ')[0] + if status == 'yes': + return True + elif status == 'no': + return False + else: + raise ValueError + except (SyntaxError, KeyError, ValueError): + raise AttributeError('Could not read pdf info.') def pdf_unlock(pdf_file, overwrite=False):