Skip to content

Commit d2f9dec

Browse files
committed
multiselect for single files, refactor, better checks
1 parent 4e3c6d4 commit d2f9dec

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
backupsheets/
55
__pycache__/
66
*.xlsx
7-
*.xls
7+
*.xls
8+
*.zip

resources/checks.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ def checkpath(path):
77
if os.path.isfile(path):
88
print(messages.isfile)
99
return False
10-
count, scount = 0, 0
10+
count, hcount = 0, 0
1111
for file in os.listdir(path):
1212
if file.endswith('.xlsx'): count += 1
13-
if file.endswith('xls'): scount +=1
14-
#other formats ...
15-
if scount > 0:
13+
split = file.rsplit('.', 1)[-1]
14+
if split.startswith('xl') and not split.endswith('x'): hcount +=1
15+
if hcount > 0:
1616
print(messages.xlshelp)
1717
if count == 0:
1818
print(messages.nofiles)
@@ -27,12 +27,21 @@ def checkfile(path):
2727
if not os.path.isfile(path):
2828
print(messages.ispath)
2929
return False
30-
if path.endswith('.xls'):
30+
split = path.rsplit('.', 1)[-1]
31+
if split.startswith('xl') and not split.endswith('x'):
3132
print(messages.oneslx)
3233
return False
33-
3434
if not path.endswith('.xlsx'):
3535
print(messages.wrongfile)
3636
return False
3737
print(messages.filegood)
3838
return True
39+
40+
def checkfiles(paths):
41+
for path in paths:
42+
if not checkfile(path):
43+
return False
44+
if len(paths) == 0:
45+
print(messages.nopath)
46+
return False
47+
return True

resources/messages.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
xlshelp = '''\033[93m
2-
You have one or more .xls files in the target directory. That file format is not supported!
2+
You have one or more unsupported excel files in the target directory. Only .xlsx is supported.
33
To use these files with this script, please follow these simple steps:
4-
1. Open the xls file in Excel
4+
1. Open the xl* file in Excel
55
2. Go to file -> Save as...
66
3. Save the file as .xlsx\n\033[0m
77
'''
88

99
oneslx = '''\033[93m
10-
Your file is of type xls. That file format is not supported!
10+
Your file is not supported. Only .xlsx is supported.
1111
To use this file with this script, please follow these simple steps:
12-
1. Open the xls file in Excel
12+
1. Open the xl* file in Excel
1313
2. Go to file -> Save as...
1414
3. Save the file as .xlsx\n\033[0m
1515
'''

unblock.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import zipfile, re, os, sys, resources.checks as checks
22
from resources.updatezip import UpdateableZipFile as uzip
3-
from tkinter.filedialog import askopenfilename, askopenfile, askdirectory
3+
from tkinter.filedialog import askopenfilenames, askdirectory
44
from tkinter import Tk
55

6-
file = not sys.argv.__contains__('--dir')
6+
files = not sys.argv.__contains__('--dir')
77
cli = sys.argv.__contains__('--cli')
8-
filepath, folderpath = '',''
8+
filepaths, folderpath = [],''
99

1010
if not cli: Tk().withdraw()
1111

1212
# input
13-
if file:
14-
filepath = askopenfilename() if not cli else input('Enter the path to the xlsx file. Both explicit and relative are valid.\n')
13+
if files:
14+
if not cli:
15+
filepaths = list(askopenfilenames(filetypes = [('excel files', '.xlsx')]))
16+
else:
17+
filepaths.append(input('Enter the path to the xlsx file. Both explicit and relative are valid.\n'))
1518
else:
1619
folderpath = askdirectory() if not cli else input('Enter the path to the target directory. Both explicit and relative are valid.\n')
1720

@@ -20,23 +23,22 @@
2023

2124
def processfile(excelfile):
2225
sheetfiles = {}
23-
2426
# get all target sheet paths
2527
with zipfile.ZipFile(excelfile) as zip:
2628
for filename in zip.namelist():
2729
if filename.startswith('xl/worksheets/sheet') and filename.endswith('.xml'):
2830
ofile = zip.open(filename)
2931
sheetfiles[filename] = re.sub(strclean, '', ofile.read().decode('utf-8'))
30-
3132
# alter files
3233
with uzip(excelfile) as file:
3334
for fname, fcontent in sheetfiles.items():
3435
file.writestr(fname, fcontent.encode('utf-8'))
3536

3637
# execute
37-
if file:
38-
if checks.checkfile(filepath):
39-
processfile(filepath)
38+
if files:
39+
if checks.checkfiles(filepaths):
40+
for filepath in filepaths:
41+
processfile(filepath)
4042
print('\033[92mFinished.\033[0m')
4143
else:
4244
if checks.checkpath(folderpath):

0 commit comments

Comments
 (0)