-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.py
More file actions
125 lines (104 loc) · 4.01 KB
/
Copy pathreplace.py
File metadata and controls
125 lines (104 loc) · 4.01 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Description: Replace text in a Word document
from os import getcwd, walk, path
import argparse
import pathlib
import logging
from tkinter import filedialog
from config import replace_data, NEW_DOC_SUFFIX
from wordreplace import replace_word
from pdfreplace import replace_pdf
from pptxreplace import replace_pptx
def get_files(directory, exts: tuple) -> list:
word_files = []
for root, dirs, files in walk(directory):
for file in files:
if file.endswith(exts) and NEW_DOC_SUFFIX not in file:
word_files.append(path.join(root, file))
return word_files
def replace_ext(fn, work_dir, exts: tuple, rdict: dict) -> None:
""" Replace text in Word documents
"""
logging.info(f'Looking for files with extension(s): {exts}...')
files = get_files(work_dir, exts)
pfl = '\n'.join(files)
logging.info(f'Found {len(files)} files:\n{pfl}\n\n')
nfiles = len(files)
for n, file in enumerate(files):
try:
logging.info(f'\nProcessing {file}...',)
progress = 100*(n+1)/nfiles
print(
f'\nProcessing {(n+1)} of {nfiles} ({progress:.2f}%) {file}...')
new_file = replace_path(work_dir, file, NEW_DOC_SUFFIX)
new_dir = pathlib.Path(new_file).parent
pathlib.Path(new_dir).mkdir(parents=True, exist_ok=True)
fn(file, new_file, rdict)
except Exception as e:
logging.error(f'{e} in {file}')
print(f'{e} in {file}')
# logging.info(f'\nProcessing {file}...',)
# print(f'\nProcessing {file}...')
# new_file = replace_path(work_dir, file, NEW_DOC_SUFFIX)
# new_dir = pathlib.Path(new_file).parent
# pathlib.Path(new_dir).mkdir(parents=True, exist_ok=True)
# fn(file, new_file, rdict)
def new_path(work_dir, ext: str):
wp = pathlib.Path(work_dir)
lst = list(wp.parts)
lst[-1] = lst[-1] + ext
wp_new = pathlib.Path(*lst)
return wp_new
def replace_path(work_dir, file, ext: str):
wp = pathlib.Path(work_dir)
lst = list(wp.parts)
lst[-1] = lst[-1] + ext
wp_new = pathlib.Path(*lst)
fl = pathlib.Path(file)
index = fl.parts.index(wp.parts[-1])
new_path = pathlib.Path(wp_new).joinpath(*fl.parts[index+1:])
return new_path
def main() -> None:
""" Main
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--dir", help="processing directory", default=None)
parser.add_argument(
"--doc", help="Process MS Word (*.docx)", action='store_true')
parser.add_argument(
"--docx", help="Process MS Word (*.docx)", action='store_true')
parser.add_argument(
"--pdf", help="Process PDF files (*.pdf)", action='store_true')
parser.add_argument(
"--pptx", help="Process Power Point files (*.pptx)", action='store_true')
args = parser.parse_args()
work_dir = args.dir
if work_dir:
work_dir.rstrip("/\\")
if not work_dir:
work_dir = filedialog.askdirectory(initialdir=getcwd(),
title="Select a Config Folder")
if not work_dir:
print('Work directory not specified or invalid!')
return
rdict = replace_data()
log_path = path.join(new_path(work_dir, NEW_DOC_SUFFIX), 'replace.log')
logging.basicConfig(filename=log_path,
encoding='utf-8', level=logging.DEBUG)
print(f'Logging to: {log_path}\n')
logging.info(work_dir)
try:
if args.doc:
replace_ext(replace_word, work_dir, ('.doc'), rdict)
if args.docx:
replace_ext(replace_word, work_dir, ('.docx'), rdict)
if args.pdf:
replace_ext(replace_pdf, work_dir, ('.pdf',), rdict)
if args.pptx:
replace_ext(replace_pptx, work_dir, ('.pptx'), rdict)
except (KeyError, ValueError, FileNotFoundError, ) as e:
logging.info(e)
logging.info('Failed!')
if __name__ == "__main__":
main()
# "C:/Users/hwn6193/OneDrive - Takeda/0.12 AAV Platform Time Capsule"