python操作xls表
1、python讀取excel中單元格內容為日期的方式
python讀取excel中單元格的內容返回的有5種類型,即上面例子中的ctype:
?1 |
ctype : 0 empty, 1 string, 2 number, 3 date, 4 boolean, 5 error
|
即date的ctype=3,這時需要使用xlrd的xldate_as_tuple來處理為date格式,先判斷表格的ctype=3時xldate才能開始操作。現在命令行看下:
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
>>> sheet2.cell( 2 , 2 ).ctype #1990/2/22
>>> sheet2.cell( 2 , 1 ).ctype #24
>>> sheet2.cell( 2 , 0 ).ctype #小胖
>>> sheet2.cell( 2 , 4 ).ctype #空值(這裏是合並單元格的原因)
>>> sheet2.cell( 2 , 2 ).value #1990/2/22
33656.0
>>> xlrd.xldate_as_tuple(sheet2.cell_value( 2 , 2 ),workbook.datemode)
( 1992 , 2 , 22 , 0 , 0 , 0 )
>>> date_value = xlrd.xldate_as_tuple(sheet2.cell_value( 2 , 2 ),workbook.datemode)
>>> date_value
( 1992 , 2 , 22 , 0 , 0 , 0 )
>>> date( * date_value[: 3 ])
datetime.date( 1992 , 2 , 22 )
>>> date( * date_value[: 3 ]).strftime( ‘%Y/%m/%d‘ )
‘1992/02/22‘
|
即可以做下簡單處理,判斷ctype是否等於3,如果等於3,則用時間格式處理:
?1 2 3 |
if (sheet.cell(row,col).ctype = = 3 ):
date_value = xlrd.xldate_as_tuple(sheet.cell_value(rows, 3 ),book.datemode)
date_tmp = date( * date_value[: 3 ]).strftime( ‘%Y/%m/%d‘ )
|
那麽問題又來了,上面 sheet2.cell(2,4).ctype 返回的值是0,說明這個單元格的值是空值,明明是合並的單元格內容"好朋友",這個是我覺得這個包功能不完善的地方,如果是合並的單元格那麽應該合並的單元格的內容一樣,但是它只是合並的第一個單元格的有值,其它的為空。
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
>>> sheet2.col_values( 4 )
[u ‘\u5173\u7cfb‘ , u ‘\u597d\u670b\u53cb‘ , ‘ ‘, u‘ \u540c\u5b66 ‘, ‘ ‘, ‘ ‘, u‘ \u4e00\u4e2a\u4eba ‘, ‘ ‘]
>>> for i in range (sheet2.nrows):
print sheet2.col_values( 4 )[i]
關系
好朋友
同學
一個人
>>> sheet2.row_values( 7 )
[u ‘\u65e0\u540d‘ , 20.0 , u ‘\u6682\u65e0‘ , ‘ ‘, ‘ ‘]
>>> for i in range (sheet2.ncols):
print sheet2.row_values( 7 )[i]
無名
20.0
暫無
>>>
|
2、讀取合並單元格的內容
這個是真沒技巧,只能獲取合並單元格的第一個cell的行列索引,才能讀到值,讀錯了就是空值。
即合並行單元格讀取行的第一個索引,合並列單元格讀取列的第一個索引,如上述,讀取行合並單元格"好朋友"和讀取列合並單元格"暫無"只能如下方式:
?1 2 3 4 5 6 7 |
>>> print sheet2.col_values( 4 )[ 1 ]
好朋友
>>> print sheet2.row_values( 7 )[ 2 ]
暫無
>>> sheet2.merged_cells # 明明有合並的單元格,為何這裏是空
[]
|
疑問又來了,合並單元格可能出現空值,但是表格本身的普通單元格也可能是空值,要怎麽獲取單元格所謂的"第一個行或列的索引"呢?
這就要先知道哪些是單元格是被合並的!
3、獲取合並的單元格
讀取文件的時候需要將formatting_info參數設置為True,默認是False,所以上面獲取合並的單元格數組為空,
?1 2 3 4 |
>>> workbook = xlrd.open_workbook(r ‘F:\demo.xlsx‘ ,formatting_info = True )
>>> sheet2 = workbook.sheet_by_name( ‘sheet2‘ )
>>> sheet2.merged_cells
[( 7 , 8 , 2 , 5 ), ( 1 , 3 , 4 , 5 ), ( 3 , 6 , 4 , 5 )]
|
merged_cells返回的這四個參數的含義是:(row,row_range,col,col_range),其中[row,row_range)包括row,不包括row_range,col也是一樣,即(1, 3, 4, 5)的含義是:第1到2行(不包括3)合並,(7, 8, 2, 5)的含義是:第2到4列合並。
利用這個,可以分別獲取合並的三個單元格的內容:
?1 2 3 4 5 6 |
>>> print sheet2.cell_value( 1 , 4 ) #(1, 3, 4, 5)
好朋友
>>> print sheet2.cell_value( 3 , 4 ) #(3, 6, 4, 5)
同學
>>> print sheet2.cell_value( 7 , 2 ) #(7, 8, 2, 5)
暫無
|
發現規律了沒?是的,獲取merge_cells返回的row和col低位的索引即可! 於是可以這樣一勞永逸:
?1 2 3 4 5 6 7 8 9 10 11 12 13 |
>>> merge = []
>>> for (rlow,rhigh,clow,chigh) in sheet2.merged_cells:
merge.append([rlow,clow])
>>> merge
[[ 7 , 2 ], [ 1 , 4 ], [ 3 , 4 ]]
>>> for index in merge:
print sheet2.cell_value(index[ 0 ],index[ 1 ])
暫無
好朋友
同學
>>>
|
python寫excel——xlwt
寫excel的難點可能不在構造一個workbook的本身,而是填充的數據,不過這不在範圍內。在寫excel的操作中也有棘手的問題,比如寫入合並的單元格就是比較麻煩的,另外寫入還有不同的樣式。這些要看源碼才能研究的透。
我"構思"了如下面的sheet1,即要用xlwt實現的東西:
基本上看起來還算復雜,而且看起來"很正規",完全是個人杜撰。
代碼如下:
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
‘‘‘
設置單元格樣式
‘‘‘
def set_style(name,height,bold = False ):
style = xlwt.XFStyle() # 初始化樣式
font = xlwt.Font() # 為樣式創建字體
font.name = name # ‘Times New Roman‘
font.bold = bold
font.color_index = 4
font.height = height
# borders= xlwt.Borders()
# borders.left= 6
# borders.right= 6
# borders.top= 6
# borders.bottom= 6
style.font = font
# style.borders = borders
return style
#寫excel
def write_excel():
f = xlwt.Workbook() #創建工作簿
‘‘‘
創建第一個sheet:
sheet1
‘‘‘
sheet1 = f.add_sheet(u ‘sheet1‘ ,cell_overwrite_ok = True ) #創建sheet
row0 = [u ‘業務‘ ,u ‘狀態‘ ,u ‘北京‘ ,u ‘上海‘ ,u ‘廣州‘ ,u ‘深圳‘ ,u ‘狀態小計‘ ,u ‘合計‘ ]
column0 = [u ‘機票‘ ,u ‘船票‘ ,u ‘火車票‘ ,u ‘汽車票‘ ,u ‘其它‘ ]
status = [u ‘預訂‘ ,u ‘出票‘ ,u ‘退票‘ ,u ‘業務小計‘ ]
#生成第一行
for i in range ( 0 , len (row0)):
sheet1.write( 0 ,i,row0[i],set_style( ‘Times New Roman‘ , 220 , True ))
#生成第一列和最後一列(合並4行)
i, j = 1 , 0
while i < 4 * len (column0) and j < len (column0):
sheet1.write_merge(i,i + 3 , 0 , 0 ,column0[j],set_style( ‘Arial‘ , 220 , True )) #第一列
sheet1.write_merge(i,i + 3 , 7 , 7 ) #最後一列"合計"
i + = 4
j + = 1
sheet1.write_merge( 21 , 21 , 0 , 1 ,u ‘合計‘ ,set_style( ‘Times New Roman‘ , 220 , True ))
#生成第二列
i = 0
while i < 4 * len (column0):
for j in range ( 0 , len (status)):
sheet1.write(j + i + 1 , 1 ,status[j])
i + = 4
f.save( ‘demo1.xlsx‘ ) #保存文件
if __name__ = = ‘__main__‘ :
#generate_workbook()
#read_excel()
write_excel()
|
需要稍作解釋的就是write_merge方法:
write_merge(x, x + m, y, w + n, string, sytle)
x表示行,y表示列,m表示跨行個數,n表示跨列個數,string表示要寫入的單元格內容,style表示單元格樣式。其中,x,y,w,h,都是以0開始計算的。
這個和xlrd中的讀合並單元格的不太一樣。
如上述:sheet1.write_merge(21,21,0,1,u‘合計‘,set_style(‘Times New Roman‘,220,True))
即在22行合並第1,2列,合並後的單元格內容為"合計",並設置了style。
如果需要創建多個sheet,則只要f.add_sheet即可。
如在上述write_excel函數裏f.save(‘demo1.xlsx‘) 這句之前再創建一個sheet2,效果如下:
代碼也是真真的easy的了:
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
‘‘‘
創建第二個sheet:
sheet2
‘‘‘
sheet2 = f.add_sheet(u ‘sheet2‘ ,cell_overwrite_ok = True ) #創建sheet2
row0 = [u ‘姓名‘ ,u ‘年齡‘ ,u ‘出生日期‘ ,u ‘愛好‘ ,u ‘關系‘ ]
column0 = [u ‘小傑‘ ,u ‘小胖‘ ,u ‘小明‘ ,u ‘大神‘ ,u ‘大仙‘ ,u ‘小敏‘ ,u ‘無名‘ ]
#生成第一行
for i in range ( 0 , len (row0)):
sheet2.write( 0 ,i,row0[i],set_style( ‘Times New Roman‘ , 220 , True ))
#生成第一列
for i in range ( 0 , len (column0)):
sheet2.write(i + 1 , 0 ,column0[i],set_style( ‘Times New Roman‘ , 220 ))
sheet2.write( 1 , 2 , ‘1991/11/11‘ )
sheet2.write_merge( 7 , 7 , 2 , 4 ,u ‘暫無‘ ) #合並列單元格
sheet2.write_merge( 1 , 2 , 4 , 4 ,u ‘好朋友‘ ) #合並行單元格
f.save( ‘demo1.xlsx‘ ) #保存文件
|
python操作xls表