-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir AdvZIP docx.py
90 lines (69 loc) · 2.55 KB
/
dir AdvZIP docx.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
"""
Opens a folder, and recursively feeds all .docx files in it to advzip.exe for recompression and reducing file size.
advzip.exe is available from https://github.com/amadvance/advancecomp
WARNING:
---------
Source files are replaced! No backup, no renaming!
"""
__author__ = 'Ilya Razmanov'
__copyright__ = '(c) 2024 Ilya Razmanov'
__credits__ = 'Ilya Razmanov'
__license__ = 'unlicense'
__version__ = '2024.02.27'
__maintainer__ = 'Ilya Razmanov'
__email__ = 'ilyarazmanov@gmail.com'
__status__ = 'Production'
import subprocess
from glob import glob
from os import name
from tkinter import Label, Tk, filedialog
# --------------------------------------------------------------
# Creating dialog
sortir = Tk()
sortir.title('Recompressing .docx...')
sortir.geometry('+100+100')
sortir.maxsize(800, 600)
zanyato = Label(sortir, wraplength=700, text='Starting...', font=('arial', 12), padx=16, pady=10, justify='center')
zanyato.pack()
sortir.withdraw()
# Main dialog created and hidden
# --------------------------------------------------------------
# Open source dir
sourcedir = filedialog.askdirectory(title='Open DIR to compress DOCX files')
if sourcedir == '':
sortir.destroy()
quit()
# --------------------------------------------------------------
# Updating dialog
sortir.deiconify()
zanyato.config(text='Allons-y!')
sortir.update()
sortir.update_idletasks()
# Dialog shown and updated
# --------------------------------------------------------------
# --------------------------------------------------------------
# Creating subprocess startupinfo for hidden console. Requires
# from os import name
#
startupinfo = None
if name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#
# now .popen or .run may be used as below:
# subprocess.run(f'program.exe -switches "{filename}"', startupinfo=startupinfo)
# --------------------------------------------------------------
# Process file list
for filename in glob(sourcedir + '/**/*.docx', recursive=True): # select all files in all subfolders
zanyato.config(text=f' Processing {filename}... ') # Updating label, showing processed file name
sortir.update()
sortir.update_idletasks()
# output in quotes for paths with spaces
subprocess.run(f'advzip.exe -q -z -4 -i 30 "{filename}"', startupinfo=startupinfo)
# --------------------------------------------------------------
# Destroying dialog
sortir.destroy()
sortir.mainloop()
# Dialog destroyed and closed
# --------------------------------------------------------------