python 写个处理Excle的脚本

import os
import xlrd,sys
#
FilePath=input("intput filepath :")
# input the searching string and column
testin=input('input the string:')
#find the cols and save to a txt
testout=input('input the string:')
outputfilename=testout + '.txt'
outputfile=open(outputfilename,'w+')
xlslist = []

# input the excel file
for root, dirs, files in os.walk(FilePath, topdown=False):
for name in files:
j=(os.path.join(root, name))
xlslist.append(j)

for Filename in xlslist :

# input the excel file
if not os.path.isfile(Filename):
print("%s is not a valid filename"%Filename)

#open the excel file
bk=xlrd.open_workbook(Filename)

#get the sheets number
shxrange=range(bk.nsheets)
# print (shxrange)

#get the sheets name
for x in shxrange:
p=bk.sheets()[x].name.encode('utf-8')
# print ("Sheets Number(%s): %s" %(x,p.decode('utf-8')))

# input your sheets name
sname=2

try:
sh=bk.sheets()[sname]
except:
print ("no this sheet")
#return None

nrows=sh.nrows
ncols=sh.ncols
# return the lines and col number
# print ("line:%d col:%d" %(nrows,ncols))

#input the check column
columnnum=0
while columnnum+1>ncols:
columnnum=int(input('your num is out of range,pls input again:'))

#find the rows which you want to select and write to a txt file
for i in range(nrows):
cell_value=sh.cell_value(i, columnnum)
if testin in str(cell_value):
print (Filename)
outputfile.write('%s' %(Filename))
outputfile.write('%s' %(os.linesep))
outputs=sh.row_values(i)
for tim in outputs:
outputfile.write('%s ' %(tim))
outputfile.write('%s' %(os.linesep))
outputfile.close()

猜你喜欢

转载自www.cnblogs.com/zzas0/p/12721324.html