網(wǎng)上已經(jīng)有很多這類的文章了,今天寫這個就算是對今天的寫的這個腳本的一個鞏固和說明吧,話說同事每個月末都要從大量的excel表格中導(dǎo)出一點點數(shù)據(jù)并輸出到另一個表格中,所以想啊,寫個腳本自動化一點吧,不然每次都會浪費(fèi)時間。
計劃有三步:
1、將眾多的excel表格的名稱輸出到namexls文件中
2、根據(jù)namexls文件名循環(huán)打開xls文件讀取相應(yīng)位置的數(shù)據(jù)存儲到列表里
3、打開要導(dǎo)入的文件并將列表里面的內(nèi)容存儲到excel表格中?
很簡單
初始模塊
#!/bin/env python?
import xlrd,xlwt,xlutils,os,sys? ?
# xlrd python從xls讀取控件? ?xlwt python 向xls寫入控件? xlutils 是xlrd xlwt的工具箱
#os模塊提供對linux操作系統(tǒng)的操作的函數(shù)? sys模塊負(fù)責(zé)程序與python解釋器的交互
from xlrd import open_workbook
from xlutils.copy import copy
reload(sys)
sys.setdefaultencoding('utf-8')
#設(shè)置字符集為utf-8格式并 reload循環(huán)加載 為啥要循環(huán)加載呢 因為啊每次加載完sys后,setdefaultencoding方法會被刪除掉所以要循環(huán)加載,you know
1、將眾多的excel表格的名稱輸出到namexls文件中
path = os.getcwd() #獲取當(dāng)前路徑
path = (path + '/excel/') #獲取眾多excel表格路徑即當(dāng)前路徑下的excel目錄里
f = open('namexls','wb')
for filename in os.listdir(path):
? ? ? ? ? ? ? ? f.write(filename)
? ? ? ? ? ? ? ? f.write('\n')
f.close()??
2、根據(jù)namexls文件名循環(huán)打開xls文件讀取相應(yīng)位置的數(shù)據(jù)存儲到列表里
f = open('namexls')
lines = f.readlines()
for line in lines:
? ? ? ? ? ? ? ? ? ?data = xlrd.open_workbook(path + line).strip()? #打開xls文件 strip 去掉頭尾指定字符
? ? ? ? ? ? ? ? ? ?table = data.sheets()[0]? #打開xls文件里面第一個表
? ? ? ? ? ? ? ? ? ?nrows = table.nrows
? ? ? ? ? ? ? ? ? ? ncols = table.ncols? ? #統(tǒng)計第一個表的行數(shù)和列數(shù)?
? ? ? ? ? ? ? ? ? ? ?rownames =? table.row_values(2)
? ? ? ? ? ? ? ? ? ? ?rownames1 =? table.row_values(3) #過去第一個表里面的第二行值和第三行值分別給? ? ? ? ? ? ? ? ? ? ? 變量rownames rownames1
? ? ? ? ? ? ? ? ? ? ?list1 = []
? ? ? ? ? ? ? ? ? ? ?list2 = []? ? ?#定義兩個列表用于接受兩個變量rownames rowname1的值? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ?for? i in range(1,ncols):? #i 從1循環(huán)到最后一列
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if? rownames[i]:? # 如果第二行里面某列值不為空則執(zhí)行下面命令
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?list1.append((rownames[i]))? #將第二行各列不為空的值添加到列表list1當(dāng)中
? ? ? ? ? ? ? ? ? ? for i in range(1,ncols):? #同理
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if? rownames1[i]:#同理
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? list2.append((rownames1[i])) #同理
? ? ? ? ? ? ? ? ? ? ?svnurl = (('url' + list1[2]).strip()+'_SVN')? #將字符串和列表list1的第三個元素拼接獲取VN庫路徑
? ? ? ? ? ? ? ? ? ? list3 = [] #定義一個空列表來接收list1和list2內(nèi)某些特定元素?
? ? ? ? ? ? ? ? ? ? list1[1] = list1[2]
? ? ? ? ? ? ? ? ? ? ?list[2]? = svnurl
? ? ? ? ? ? ? ? ? ? ? list3.append(u'項目交付部')
? ? ? ? ? ? ? ? ? ? ? list3.append(list1[1])
? ? ? ? ? ? ? ? ? ? ? list3.append(list1[0])
? ? ? ? ? ? ? ? ? ? ? list3.append(list2[0])
? ? ? ? ? ? ? ? ? ? ? ?list3.append(list1[2])? # 以上為list1和list2列表中特定元素輸出到list3中
3、打開要導(dǎo)入的文件并將列表里面的內(nèi)容存儲到excel表格中
? ? ? ? ? ? ? ? ? ? ? rexcel = open_workbook('項目配置庫臺賬2017.xlsx',formatting_info=True) #打開要輸入的excel表格? formatting_info 參數(shù)保證原有表格格式不變化
? ? ? ? ? ? ? ? ? ? ?rows = rexcel.sheets()[0].nrows
? ? ? ? ? ? ? ? ? ? ? cols = recel.sheets()[0].ncols? ?#統(tǒng)計表中行數(shù)和列數(shù)
? ? ? ? ? ? ? ? ? ? ? ?excel = copy(rexcel)? ?#copy表
? ? ? ? ? ? ? ? ? ? ? ?table = excel.get_sheet(0)? ?#獲取第一張表
? ? ? ? ? ? ? ? ? ? ? ?j = 0?
? ? ? ? ? ? ? ? ? ? ? ?for? i in list3:? #i變量循環(huán)讀取list3里面的數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?table.write(rows,j,i)? #對最后一行第j列追加list3的數(shù)據(jù)i?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? j += 1? ?#第一列追加完成后加1對第二列進(jìn)行追加
? ? ? ? ? ? ? ? ? ? ? ?row += 1? ?#namexls文件中第一個xls文件關(guān)鍵數(shù)據(jù)執(zhí)行完后換行執(zhí)行第二個xls文件關(guān)鍵數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? excel.save(''項目配置庫臺賬2017.xlsx)? #對文件進(jìn)行保存操作
至此眾多excel表格中關(guān)鍵性數(shù)據(jù)追加項目配置庫臺賬2017.xlsx完成,每個月只要將眾多excel表格放置/home/CMstandingbook/excel 執(zhí)行CMstandingbook.py 便可實現(xiàn)配置庫臺賬的自動更新
源碼如下:
#!/bin/env python
import xlrd,xlwt,xlutils,os,sys
from xlrd import open_workbook
from xlutils.copy import copy
reload(sys)
sys.setdefaultencoding('utf-8')
path = os.getcwd()
path = path +'/excel/'
f = open('namexls','wb')
for fliename in os.listdir(path):
f.write(fliename)
f.write('\n')
f.close()
f = open('namexls')
lines = f.readlines()
for line in lines:
data = xlrd.open_workbook((path + line).strip())
table = data.sheets()[0]
nrows =? table.nrows
ncols =? table.ncols
colnames = table.row_values(2)
colnames2 = table.row_values(3)
list1 = []
list2 = []
for i in? range(1,ncols):
if colnames[i]:
list1.append(colnames[i])
for i in? range(1,ncols):
if colnames2[i]:
list2.append(colnames2[i])
svnurl = (('https://111.200.54.229:8443/svn/'+list1[2]).strip()+'_SVN')
list1[1] = list1[2]
list1[2] = svnurl
list3 = []
list3.append(u'項目交付部')
list3.append(list1[1])
list3.append(list1[0])
list3.append(list2[0])
list3.append(list1[2])
rexcel = open_workbook('項目配置庫臺賬2017.xlsx',formatting_info=True)
rows = rexcel.sheets()[0].nrows
cols = rexcel.sheets()[0].ncols
excel = copy(rexcel)
table = excel.get_sheet(0)
print cols
print rows
j? = 0
for? i in list3:
table.write(rows,j,i)
j += 1
rows +=1
excel.save('項目配置庫臺賬2017.xlsx')