-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunblock.py
48 lines (42 loc) · 1.69 KB
/
unblock.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
import zipfile, re, os, sys, resources.checks as checks
from resources.updatezip import UpdateableZipFile as uzip
from tkinter.filedialog import askopenfilenames, askdirectory
from tkinter import Tk
files = not sys.argv.__contains__('--dir')
cli = sys.argv.__contains__('--cli')
filepaths, folderpath = [],''
if not cli: Tk().withdraw()
# input
if files:
if not cli:
filepaths = list(askopenfilenames(filetypes = [('excel files', '.xlsx')]))
else:
filepaths.append(input('Enter the path to the xlsx file. Both explicit and relative are valid.\n'))
else:
folderpath = askdirectory() if not cli else input('Enter the path to the target directory. Both explicit and relative are valid.\n')
#regex pattern to exclude sheetProtection
strclean = re.compile('<sheetProtection.*?>')
def processfile(excelfile):
sheetfiles = {}
# get all target sheet paths
with zipfile.ZipFile(excelfile) as zip:
for filename in zip.namelist():
if filename.startswith('xl/worksheets/sheet') and filename.endswith('.xml'):
ofile = zip.open(filename)
sheetfiles[filename] = re.sub(strclean, '', ofile.read().decode('utf-8'))
# alter files
with uzip(excelfile) as file:
for fname, fcontent in sheetfiles.items():
file.writestr(fname, fcontent.encode('utf-8'))
# execute
if files:
if checks.checkfiles(filepaths):
for filepath in filepaths:
processfile(filepath)
print('\033[92mFinished.\033[0m')
else:
if checks.checkpath(folderpath):
for item in os.listdir(folderpath):
if item.endswith('.xlsx'):
processfile(item)
print('\033[92mFinished.\033[0m')