python操作excel让工作自动化 某局某领导给了3只excel文件,一只里面有4个sheet需要处理,一个sheet有250+列,算下来总共有3000+列需要手动反复插入、删除列、拷贝、求和,所以给了4天的时间要完成。 我不愿意做大量简单而且又是重复性工作,看了看3只表格的格式基本一样,于是我用python写了30行代码完成了这个“艰巨”任务。 0x01 用python操作excel需要安装xlrd、xlwt(或者其他的模块也行)。 读取excel文件:workbook = xlrd.open_workbook('filename.xlsx') 获取所有表名:sheet_names = workbook.sheet_names() 通过索引顺序获取一个工作表:sheet0 = workbook.sheets()[0] || sheet1 = workbook.sheet_by_index(1) 通过名称获取一个工作表:sheet3= data.sheet_by_name(u'sheetname') 获取表的行、列大小:rows = sheet.nrows || cols = sheet.ncols 获取指定行、列的值:col0_value = sheet.col_valsue(0) || row0_value = sheet.row_values(0) 创建工作对象:wk = xlwt.Workbook(encoding='utf-8') 添加sheet对象:sheet0 = wk.add_sheet(sheetname,cell_overwrite=True) 将值写入单元格:sheet0.write(row,col,value) 保存文件:wk.save('filename.xls') 0x02 # coding=utf-8 import xlrd import xlwt workbook = xlrd.open_workbook('2014年排放量.xlsx') sheet_names = workbook.sheet_names() #通过索引顺序获取一个工作表 sheet0 = workbook.sheets()[0] sheet1 = workbook.sheet_by_index(1) sheet2 = workbook.sheet_by_index(2) sheet3 = workbook.sheet_by_index(3) sheet4 = workbook.sheet_by_index(4) sheet5 = workbook.sheet_by_index(5) sheet6 = workbook.sheet_by_index(6) sheet7 = workbook.sheet_by_index(7) sheet8 = workbook.sheet_by_index(8) #通过名称获取一个工作表 #table = data.sheet_by_name(u'Sheet1') #获取多少行、列 sht0_rows = sheet0.nrows sht0_cols = sheet0.ncols #获取指定单元格的值 temp = sheet0.cell(0,2) #获取指定行、列的值 temp_col0 = sheet0.col_values(0) temp_row0 = sheet0.row_values(0) temp_col1 = sheet0.col_values(1) temp_row1 = sheet0.row_values(1) #需要204个单元格 一共tb0_rows个 减去前两个 ''' for i in range(204/6):#循环34次 for j in temp_row: j[2:8] ''' #print(tb0_rows,tb0_cols,temp,temp_row,temp_row[2:8],temp_col) #取选定工作范围 #print(temp_row[2:206]) wk = xlwt.Workbook(encoding='utf-8') wk_lst = []#创建sheet列表 for shtname in sheet_names: #print(shtname) wk_lst.append(wk.add_sheet(shtname,cell_overwrite_ok=True)) #print(len(wk_lst)) '''测试插入表格 for c in range(len(temp_col0)): wk_lst[0].write(c,0,temp_col0[c]) #print(temp_col[c]) ''' #需要读的sheet列表 xlrd_sheet_list = [sheet0,sheet1,sheet2,sheet3] for tm in range(len(wk_lst)): if tm<4: '''1. 创建样表''' for x in range(2): #print('----------x:',x) temp_col = sheet0.col_values(x) for c in range(len(temp_col)): #print('--------c:',c) #print(temp_col[c]) wk_lst[tm].write(c,x,temp_col[c]) temp_row = sheet0.row_values(0) for r in range(len(temp_row)-2): #print(tm,len(temp_row)) wk_lst[tm].write(0,r+2,temp_row[r+2]) '''2. 写入工作区域''' for r in range(2,13):#创建工作行 tmp_row = xlrd_sheet_list[tm].row_values(r) w = tmp_row[2:206] # 切片获取该行工作列 #print(len(w) / 6) x = 0 for i in range(int(len(w) / 6)): sum_pf = round(w[0 + x] + w[1 + x] + w[2 + x] + w[3 + x] + w[4 + x] + w[5 + x], 2) wk_lst[tm].write(r,2+x,sum_pf) print(sum_pf) x += 6 print("----------------------------------------r:",r,2+x,sum_pf) #for i in range(2,13): # print(i) wk.save('nb.xls') '''#测试创建excel文件 wkt = xlwt.Workbook() ws = wkt.add_sheet('CO') ws.write(0,0,'1') wkt.save('fuck.xls') ''' ''' #临时注释 一会儿放开 for r in range(2,13):#创建工作行 tmp_row = sheet0.row_values(r) w = tmp_row[2:206] # 切片获取该行工作列 #print(len(w) / 6) x = 0 y = 0 for i in range(int(len(w) / 6)): #wk_lst[0].write(2+) print(round(w[0 + x] + w[1 + x] + w[2 + x] + w[3 + x] + w[4 + x] + w[5 + x], 2)) x += 6 print("----------------------------------------",r) ''' '''#测试切片 相加 w = temp_row[2:206]#切片获取 print(len(w)/6) x=0 for i in range(int(len(w)/6)): print(round(w[0+x]+w[1+x]+w[2+x]+w[3+x]+w[4+x]+w[5+x],2)) x+=6 ''' 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中文源码网。