剛入門的小白用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
"""
*聲明:本文於網路整理,版權歸原作者所有,如來源信息有誤或侵犯權益,請聯繫我們刪除或授權事宜。
覺得不錯,點個「在看」然後轉發出去
※為什麼你學Python比別人慢?因為你沒有這套完整的學習資料
※PyCharm是什麼,為什麼推薦給Python用戶?
TAG:python |