Last active
December 18, 2022 16:19
-
-
Save RahulDas-dev/af27ff5aee8cc264e5a9e1c3dedc7865 to your computer and use it in GitHub Desktop.
PDF File Password removal using PIKEPDF package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os | |
import pikepdf | |
perser = argparse.ArgumentParser() | |
perser.add_argument('Path', metavar='path', help='PDF directory') | |
args = perser.parse_args() | |
if os.path.isdir(args.Path) is False: | |
raise NotADirectoryError(f'Directory {args.Path} is Not Valid') | |
if any(map(lambda x: x.split('.')[-1] in ['pdf','PDF'], os.listdir(args.Path))) is False: | |
raise FileNotFoundError(f'No Pdf File Found on Directory {args.path}') | |
reports = [] | |
for file in os.listdir(args.Path): | |
file_name, extn = ''.join(file.split('.')[:-1]), file.split('.')[-1] | |
if extn not in ['pdf','PDF']: | |
continue | |
pdf_pass = input(f'Enter Password for PDF {file} : ') | |
print('Processing {file}...\n') | |
try: | |
pdf = pikepdf.open(os.path.join(args.Path, file), password=pdf_pass) | |
except pikepdf.PasswordError: | |
continue | |
new_path = os.path.join(args.Path, f'{file_name}_password_removed.{extn}') | |
pdf.save(new_path) | |
reports.append((file,f'{file_name}_password_removed.{extn}')) | |
print(f"{'Original File Name':<50} | {'New File Name ':<50}") | |
print('-'*102) | |
for i, j in reports: | |
print(f"{i:<50} | {j:<50}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output