您的位置:首页 > 编程语言 > Python开发

python对excle的操作

2019-06-27 00:33 197 查看
#读操作
kwb=xlrd.open_workbook("E:/123.xls")
sheet=kwb.sheets()[0]
print (kwb.sheet_names())
print (sheet.nrows)
print (sheet.ncols)
print (sheet.row_values(0))
print (sheet.col_values(0))
print (sheet.cell_value(1,1))

#写操作(全新覆盖,不推荐)
kwb=xlwt.Workbook()
sheet=kwb.add_sheet("Sheet 1",cell_overwrite_ok=True)
sheet.write(2,0,'zyw1')
sheet.write(2,1,19980706)
kwb.save('E:/123.xls')

#写操作(部分覆盖,追加)
kwb=xlrd.open_workbook('E:/123.xls')
kwb1=copy.copy(kwb)
sheet=kwb1.get_sheet(0)
sheet.write(1,0,'bbb')
sheet.write(1,1,456)
sheet.write(2,0,'ccc')
sheet.write(2,1,789)
kwb1.save('E:/123.xls')

#替换exlce表中部分内容
list=['tom','p','uu']
relist=['Caronline','1','p']
path='E:/123.xls'

kwb=xlrd.open_workbook(path)
r_sheet=kwb.sheets()[0] #读sheet
kwb1=copy.copy(kwb)
w_sheet=kwb1.get_sheet(0) #写sheet
for i in range(r_sheet.nrows):
n = 0
L=r_sheet.row_values(i)
for j in L:
j=str(j)
for k in range(len(list)):
if list[k] in j:
s=j.replace(list[k],relist[k])
w_sheet.write(i,n,s)
n+=1
kwb1.save(path)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: