import
glob
from
collections
import
Counter
import
re
import
xlwt
from
xlwt
import
Workbook
import
xlsxwriter
import
xlrd
import
errno
import
time
from
datetime
import
datetime
import
datetime
import
os
import
os.path
import
warnings
from
xlutils.copy
import
copy
import
openpyxl
from
xlrd
import
open_workbook
warnings.filterwarnings(
"ignore"
)
timestr
=
time.strftime(
"%Y%m%d-%H%M%S"
)
yourpath
=
"D:\\mainfolder\\subfolders"
loc
=
(
"D:\\sample.xlsx"
)
cnt
=
Counter()
wb
=
xlrd.open_workbook(loc)
sheet
=
wb.sheet_by_index(
0
)
rows
=
sheet.nrows
excel_word
=
[]
for
i
in
range
(
1
,rows):
excel_word.append(sheet.cell_value(i,
1
))
report_txt
=
"D:\\mainfolder\\report"
+
timestr
+
".txt"
FO
=
open
(report_txt,
'w'
)
str3
=
"|"
+
"Pattern"
+
" "
*
(
20
-
len
(
"Pattern"
))
+
"|"
+
"Vuernabilitiy in file"
+
" "
*
(
200
-
len
(
"Vuernabilitiy in file"
))
+
"|"
+
"Line No"
+
" "
*
(
10
-
len
(
"Line No"
))
+
"|"
+
"\n"
FO.write(str3)
FO.write(
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
+
"\n"
)
for
root, dirs, files
in
os.walk(yourpath, topdown
=
False
):
line
=
0
for
name
in
files:
path
=
os.path.join(root, name)
files
=
glob.glob(path)
for
name
in
files:
try
:
with
open
(name,encoding
=
"utf8"
,errors
=
'ignore'
) as f:
text_string1
=
f.read()
for
i
in
range
(
0
,
len
(excel_word)):
str2
=
''
for
num, line
in
enumerate
(name,
1
):
if
excel_word[i]
in
text_string1:
cnt[excel_word[i]]
+
=
1
str2
=
"|"
+
excel_word[i]
+
" "
*
(
20
-
len
(excel_word[i]))
+
"|"
+
os.path.join(root, name)
+
" "
*
(
200
-
len
(os.path.join(root, name)))
+
"|"
+
str
(num)
+
" "
*
(
10
-
len
(
str
(num)))
+
"|"
+
"\n"
else
:
cnt[excel_word[i]]
+
=
0
FO.write(str2)
except
IOError as exc:
if
exc.errno !
=
errno.EISDIR:
raise
FO.close()