打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
刚入门的小白用Python操作excel表格!使工作效率提升一倍不止!

前言

某局某领导给了3只excel文件,一只里面有4个sheet需要处理,一个sheet有250 列,算下来总共有3000 列需要手动反复插入、删除列、拷贝、求和,所以给了4天的时间要完成。

我不愿意做大量简单而且又是重复性工作,看了看3只表格的格式基本一样,于是我用python写了30行代码完成了这个“艰巨”任务。

关注后私信小编 PDF领取十套电子文档书籍

用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')

---------------------

# coding=utf-8import xlrdimport 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.nrowssht0_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=0for 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'''
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用Python处理excel表格(openpyxl)教程
如何利用Python编程批量处理Excel来提高日常工作效率!
Python操作Excel之xlsx文件
化繁为简,python操作excel
Python Excel vlookup函数实现过程解析
操作excel的xlwt库难道没有删除sheet 的方法吗?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服