forked from Hooodo/DocumentsTextSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalkxls.py
73 lines (65 loc) · 1.56 KB
/
walkxls.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import sys
from pyExcelerator import *
#mypath = 'E:\\Work\\E_\\xxx\\xx'
mypath = 'E:\\Work\\qqqqqqqqqqq'
#mypath = 'E:\\PyPro'
pat1 = 'Accounting'
pattern1 = re.compile(pat1, re.I|re.M)
pat2 = 'RADIUS'
pattern2 = re.compile(pat2, re.I|re.M)
pat3 = 'ACCT-SRV'
pattern3 = re.compile(pat3, re.I|re.M)
def walkdir(path):
files = os.listdir(path)
for file in files:
fullpath = path+'\\'+file
if os.path.isdir(fullpath):
walkdir(fullpath)
else:
sufix = os.path.splitext(fullpath)[1][1:]
if sufix == 'xls':
print fullpath
processdoc(fullpath)
def processdoc(filename):
#xls
savef = open('E://temp.txt', 'w')
try:
for sheet_name, values in parse_xls(filename, 'cp1251'):
buf = 'Sheet = "%s"' % sheet_name.encode('cp866', 'backslashreplace')
buf += '\n'
savef.write(buf)
for row_idx, col_idx in sorted(values.keys()):
v = values[(row_idx, col_idx)]
if isinstance(v, unicode):
v = v.encode('cp866', 'backslashreplace')
else:
v = `v`
buf = v+'\n'
savef.write(buf)
except:
print 'process fail.'
savef.close()
#text
flag = False
file = open('E://temp.txt', 'r')
buff = file.read()
if pattern1.search(buff) != None:
print 'has ' + pat1
flag = True
if pattern2.search(buff) != None:
print 'has ' + pat2
flag = True
if pattern3.search(buff) != None:
print 'has ' + pat3
flag = True
file.close()
if flag:
logfile = open('E://log.txt', 'a')
logfile.write('**'+filename)
logfile.write('\r\n')
logfile.close()
walkdir(mypath)