python操作excel讓工作自動化
某局某領(lǐng)導(dǎo)給了3只excel文件,一只里面有4個sheet需要處理,一個sheet有250+列,算下來總共有3000+列需要手動反復(fù)插入、刪除列、拷貝、求和,所以給了4天的時間要完成。
我不愿意做大量簡單而且又是重復(fù)性工作,看了看3只表格的格式基本一樣,于是我用python寫了30行代碼完成了這個“艱巨”任務(wù)。
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')
獲取表的行、列大?。簉ows = sheet.nrows || cols = sheet.ncols
獲取指定行、列的值:col0_value = sheet.col_valsue(0) || row0_value = sheet.row_values(0)
創(chuàng)建工作對象: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):#循環(huán)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 = []#創(chuàng)建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. 創(chuàng)建樣表'''
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. 寫入工作區(qū)域'''
for r in range(2,13):#創(chuàng)建工作行
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')
'''#測試創(chuàng)建excel文件
wkt = xlwt.Workbook()
ws = wkt.add_sheet('CO')
ws.write(0,0,'1')
wkt.save('fuck.xls')
'''
'''
#臨時注釋 一會兒放開
for r in range(2,13):#創(chuàng)建工作行
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
'''
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解APScheduler如何設(shè)置任務(wù)不并發(fā)
本文主要介紹了APScheduler如何設(shè)置任務(wù)不并發(fā),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
徹底吃透理解Python基礎(chǔ)33個關(guān)鍵字詳細(xì)教程
這篇文章主要為大家介紹了徹底吃透理解Python中33個關(guān)鍵字的詳細(xì)教程,有需要打好Python基礎(chǔ)的同學(xué)可以借鑒參考下,希望能成為您成功路上的一塊墊腳石2021-10-10
Python中encode和encoding的區(qū)別小結(jié)
encode和encoding在Python中雖然都與字符編碼相關(guān),但它們的角色和用途是不同的,本文主要介紹了Python中encode和encoding的區(qū)別小結(jié),具有一定的參考價值,感興趣的可以了解一下2024-03-03

