-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
96 lines (79 loc) · 3.25 KB
/
main.py
File metadata and controls
96 lines (79 loc) · 3.25 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pikepdf
import os
import argparse
def argument_parser():
parser = argparse.ArgumentParser(description = "Lock or unlock your PDF")
config = parser.add_mutually_exclusive_group()
config.add_argument("-f", "--file", help = "Use the configuration file", action = "store_true")
config.add_argument("-c", "--command_line", help = "Configure the cript using the command line", action = "store_true")
selection = parser.add_mutually_exclusive_group()
selection.add_argument("-l", "--lock", help = "Lock your PDF(s) with password", action = "store_true")
selection.add_argument("-u", "--unlock", help = "Unlock your PDF(s)", action = "store_true")
args = parser.parse_args()
return parser, args.lock, args.unlock, args.file, args.command_line
def read_configuration_file(file_name):
file = open(file_name, "r")
lines = file.readlines()
source_folder = lines[0].replace("\n", "")
destination_folder = lines[1].replace("\n", "")
password = lines[2].replace("\n", "")
file.close()
return source_folder, destination_folder, password
def create_destination_folder(destination_folder):
try:
os.mkdir(destination_folder)
print("Directory", destination_folder, "created")
except FileExistsError:
print("Directory", destination_folder, "already exists")
def unlock_PDf(source_folder, destination_folder, password):
count = 0
for item in os.scandir(source_folder):
if ".pdf" in item.name:
file_name = item.name
try:
mypdf = pikepdf.open(source_folder+"/"+file_name, password = password) # open the locked pdf in source folder
mypdf.save(destination_folder+"/"+file_name) # save the unlocked pdf in destination folder
except pikepdf.PasswordError:
print("The password failed to open the file!")
print ("\t\"" + file_name + "\"" + " unlocked")
count = count + 1
return count
def lock_PDf(source_folder, destination_folder, password):
count = 0
for item in os.scandir(source_folder):
if ".pdf" in item.name:
file_name = item.name
mypdf = pikepdf.open(source_folder+"/"+file_name) # open the unlocked pdf in source folder
mypdf.save(destination_folder+"/"+file_name, encryption = pikepdf.Encryption(owner = password, user = password)) # save the locked pdf in destination folder
print ("\t\"" + file_name + "\"" + " locked")
count = count + 1
return count
def resume_operations(count):
if (count < 2 and count != 0):
print ("\tFile processed:", count)
else:
print ("\tFiles processed:", count)
def main():
parser, lock, unlock, file, command_line = argument_parser()
if file:
# read configuration file
source_folder, destination_folder, password = read_configuration_file(file_name = "config.txt")
elif command_line:
source_folder = input('Insert source folder name: ')
destination_folder = input('Insert destination folder folder name: ')
password = input('Insert password: ')
else:
parser.print_help()
# Create destination folder
create_destination_folder(destination_folder)
if unlock:
# unlock all PDF(s) in source folder
count = unlock_PDf(source_folder, destination_folder, password)
elif lock:
# lock all PDF(s) in source folder
count = lock_PDf(source_folder, destination_folder, password)
else:
parser.print_help()
resume_operations(count)
if __name__ == "__main__":
main()