python 写个处理Excle的脚本

写了个处理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()

posted @   zzas12345  阅读(226)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示