-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocx_unlocker.py
68 lines (62 loc) · 2.49 KB
/
docx_unlocker.py
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
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 24 10:41:48 2018
@author: moskalev_iv
"""
import os
import shutil
import argparse
def unlock(file_name : str):
f_name = os.path.dirname(file_name) + '\\@' + os.path.basename(file_name)
shutil.unpack_archive(file_name, file_name[:-5], 'zip')
inFile = open(file_name[:-5] + '\\word\\settings.xml', 'r')
data = inFile.readlines()
inFile.close()
start = data[1].find('<w:documentProtection')
if start > 0:
end = data[1].find('/>', start)
data[1] = data[1].replace(data[1][start:end+2], '')
outFile = open(file_name[:-5] + '\\word\\settings.xml', 'w')
outFile.writelines(data)
outFile.close()
shutil.make_archive(f_name, 'zip', file_name[:-5])
os.rename(f_name + '.zip', f_name)
print("{} unlocked".format(file_name))
else:
print("File \"{}\" isn't locked".format(file_name))
shutil.rmtree(file_name[:-5])
def main():
parser = argparse.ArgumentParser()
parser.add_argument("path", help="path to docx")
parser.add_argument("-r", "--recursive", action="store_true",
help="recursive unlocked docx in path")
args = parser.parse_args()
if args.recursive:
user_answer = input("Are you want to unlock all files in \"{}\" recursively? [y/n] ".format(os.path.abspath(args.path)))
if str.lower(user_answer) == "y":
files = []
print(os.path.abspath(args.path))
for root, _, filenames in os.walk(os.path.abspath(args.path)):
files.extend([root + "\\" + file for file in filenames if ".docx" in file])
if files != []:
for f in files:
unlock(f)
else:
print("Do not exist suitable files")
else:
if os.path.isdir(args.path):
user_answer = input("Are you want to unlock all files in \"{}\"? [y/n] ".format(os.path.abspath(args.path)))
if str.lower(user_answer) == "y":
files = []
for root, _, filenames in os.walk(os.path.abspath(args.path)):
files.extend([root + "\\" + file for file in filenames if ".docx" in file])
break
if files != []:
for f in files:
unlock(f)
else:
print("Do not exist suitable files")
else:
unlock(os.path.abspath(args.path))
if __name__ == "__main__":
main()