首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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

前言

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

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

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

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

'''

*声明:本文于网络整理,版权归原作者所有,如来源信息有误或侵犯权益,请联系我们删除或授权事宜。

觉得不错,点个“在看”然后转发出去

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190702A0QFWY00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券