打開workbook
- import xlrd
- wb = xlrd.open_workbook('mywokbook.xls')
檢查表單名字
wb.sheet_names()
獲取sheet表
- sh = wb.sheet_by_index(0) # 索引獲取
- sh = wb.sheet_by_name('Sheet01') # 名字獲取
遞歸打印所有行數(shù)據(jù)
- for n in range(sh.nrows):
- print sh.row_values(n)
返回第N列數(shù)據(jù)
first_column = sh.col_values(N)
通過索引讀取某單元格數(shù)據(jù)
cell_A1 = sh.cell(0, 0).value
初始化workbook對象,之后才能進行寫入操作
- import xlwt
- wbk = xlwt.Workbook()
- sheet = wbk.add_sheet('sheet2')
寫入數(shù)據(jù)
sheet.write(0, 1, 'new text')
保存文件
wbk.save('new.xls')
注意:修改表單內(nèi)容,需要使用cell_overwrite_ok=True來創(chuàng)建worksheet
- sheet2 = wbk.add_sheet('sheet2', cell_overwrite_ok=True)
- sheet2.write(0, 0, 'text')
- sheet2.write(0, 0, 'altertext')
Python中一般使用xlrd(excel read)來讀取Excel文件,使用xlwt(excel write)來生成Excel文件(可以控制Excel中單元格的格式),需要注意的是,用xlrd讀 取excel是不能對其進行操作的:xlrd.open_workbook()方法返回xlrd.Book類型,是只讀的,不能對其進行操作。而 xlwt.Workbook()返回的xlwt.Workbook類型的save(filepath)方法可以保存excel文件。因此對于讀取和生成Excel文件都非常容易處理,但是對于已經(jīng)存在的Excel文件進行修改就比較麻煩了。不過,還有一個xlutils(依賴于xlrd和xlwt)提供復(fù)制excel文件內(nèi)容和修改文件的功能。其實際也只是在xlrd.Book和xlwt.Workbook之間建立了一個管道而已,如下圖:
xlutils.copy模塊的copy()方法實現(xiàn)這個功能
- from xlrd import open_workbook
- from xlutils.copy import copy
- rb = open_workbook('D:\\text.xls')
- # 通過get_sheet()獲取的sheet才有write()方法
- wb = copy(rb)
- ws = wb.get_sheet(0)
- ws.write(0, 0, 'changed!')
- wb.save('D:\\new.xls')
將公司采購單轉(zhuǎn)換成苗木平臺生成的Excel模板,采購單總共76條數(shù)據(jù),模板14種苗木分類。
采購數(shù)據(jù)樣式:
苗木平臺模板數(shù)據(jù)樣式:
生成的Excel表格
偷了個懶,比如時間數(shù)據(jù)的格式?jīng)]進行設(shè)置,實際代碼如下
- import re
- import xlrd
- import xlwt
- from xlutils.copy import copy
- # 打開sheet表
- def open_sheet(xl_name):
- xl = xlrd.open_workbook(xl_name)
- xl_sheet = xl.sheets()[0]
- return xl_sheet
- # 將原文件的采購行插入匹配模板的采購行
- def insert_mb(L, line_no, ws):
- for x in range(len(L)):
- one_tree = L[x]
- line_no += 1
- for i in range(mb_sheet.nrows):
- if i == 1:
- ws.write(line_no, i, tree_classify(one_tree[2]))
- if i == 2:
- ws.write(line_no, i, one_tree[2])
- if i == 3:
- ws.write(line_no, i, one_tree[7])
- if i == 4:
- ws.write(line_no, i, '43485')
- if i == 7:
- ws.write(line_no, i, tree_classify(str(one_tree[3])))
- if i == 8:
- ws.write(line_no, i, one_tree[4])
- if i == 9:
- ws.write(line_no, i, one_tree[5])
- if i == 12:
- ws.write(line_no, i, one_tree[-1])
- # 處理采購項名稱
- def tree_classify(tree_name):
- s = re.sub('[A-Z]', '', tree_name)
- s = s.split('(')[0]
- return s
- # 查找模板中苗木某種分類的行號
- def search_no(sheet, class_no):
- for i in range(len(col_data)):
- if class_no == 1 and col_data[i] == '一':
- line_no = i
- elif class_no == 2 and col_data[i] == '二':
- line_no = i
- elif class_no == 3 and col_data[i] == '三':
- line_no = i
- elif class_no == 4 and col_data[i] == '四':
- line_no = i
- elif class_no == 5 and col_data[i] == '五':
- line_no = i
- elif class_no == 6 and col_data[i] == '六':
- line_no = i
- return line_no
- # 讀取所有的采購項并按序號分類
- def load_class_no(class_no, sheet):
- L = []
- for i in range(4, sheet.nrows-6):
- one_tree = sheet.row_values(i)[:-8]
- if one_tree[0] == class_no:
- L.append(one_tree)
- return L
- # copy模板才可以修改
- def copy_alter(sheet):
- cp_sheet = copy(sheet)
- if __name__ == '__main__':
- order_name = '副本徑河項目苗木采購審批表.xlsx'
- mb_name = 'Copy of importBuyList_ex.xls'
- tree_sheet = open_sheet(order_name)
- mb_sheet = open_sheet(mb_name)
- # 模板中第一列的數(shù)據(jù)
- col_data = mb_sheet.col_values(0)
- # copy后模板才可以修改
- mb = xlrd.open_workbook(mb_name)
- cp_sheet = copy(mb)
- wr_sheet = cp_sheet.get_sheet(0)
- for no in range(1, 7):
- class_trees = load_class_no(no, tree_sheet)
- line_no = search_no(wr_sheet, no)
- insert_mb(class_trees, line_no, wr_sheet)
- cp_sheet.save('new.xls')