-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocx_search_manipulation.py
73 lines (66 loc) · 2.18 KB
/
docx_search_manipulation.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
import os
import docx
import os.path
from os import path
from docx import Document
import re
import shutil
from shutil import copyfile
# User provides file path
path1 = '123'
while path.exists(path1) == False:
path1 = input('Please enter the location of the original files:')
if path.exists(path1):
os.chdir(path1) # Set working directory
else:
print('Warning: Invalid path')
# A function that extracts files that match the pattern
def search_file(filename, pattern):
doc = docx.Document(filename)
text = []
for para in doc.paragraphs:
text.append(para.text)
if re.findall(pattern, str(text)) != []:
return filename
else:
return 0
# Search docx files with content matching particular pattern
pattern = input('Please enter the keyword')
matched_files = []
for file in os.listdir():
if not file.endswith('.docx'): continue
x = search_file(file, pattern)
if x != 0:
matched_files.append(x)
# Extract matched files path
matched_files_paths = []
for matched_file in matched_files:
path = f"{path1}\\{matched_file}"
matched_files_paths.append(path)
# A function that manipulates files (move, copy, or delete)
def file_manipulate(mode, matched_file_path, to):
if mode == 'move':
shutil.move(matched_file_path, to)
elif mode == 'copy':
shutil.copy(matched_file_path, to)
elif mode == 'delete':
os.remove(matched_file_path)
else:
return 'no such mode'
# User provides mode and new file path
mode = '123'
while mode not in ['move','copy', 'delete']:
mode = input('move, copy, or delete?')
if mode == 'delete':
to = ''
for matched_file_path in matched_files_paths:
file_manipulate(mode, matched_file_path, to)
elif mode in ['move', 'copy']:
to = input("Please provide the new file path")
if path.exists(to) == False:
print('Warning: Invalid path')
else:
for matched_file_path in matched_files_paths:
file_manipulate(mode, matched_file_path, to)
else:
print('Warning: Invalid mode')