1. 程式人生 > >Python處理Excel(二):個性化Excel表格

Python處理Excel(二):個性化Excel表格

程式碼如下

import xlwt

book=xlwt.Workbook()
xlwt.add_palette_colour('style1',8)
book.set_colour_RGB(8,189,183,107)
xlwt.add_palette_colour('style2',9)
book.set_colour_RGB(9,25,25,112)
xlwt.add_palette_colour('style3',10)
book.set_colour_RGB(10,240,230,140)
style1 = xlwt.easyxf('pattern: pattern solid, fore_colour style1,back_colour black;'
                              'font: colour style2, bold True;'
				'borders: left 0x0d,left_colour black , right 0x0d, top 0x0d, bottom 0x0d;'
				'alignment: horz center,vert center')
style2 = xlwt.easyxf(  'font: colour style2, bold False;'
				'borders: left 0x0d,left_colour black , right 0x0d, top 0x0d, bottom 0x0d;'
				'alignment: horz center,vert center')
style3 = xlwt.easyxf('pattern: pattern solid, fore_colour style3,back_colour black;'
                              'font: colour style2, bold True;'
				'borders: left 0x0d , right 0x0d, top 0x0d, bottom 0x0d;'
				'alignment: horz center,vert center')

sheet=book.add_sheet('new',cell_overwrite_ok=True)

sheet.write(0,0,'Scenario \nVideo Play',style1)
sheet.row(0).height=500
sheet.write_merge(0,0,1,6,'BW Requirement',style1)
sheet.write_merge(1,3,0,0,'iSharkL2\n estimate \nBW(MB/s)',style2)
sheet.row(2).height=800
sheet.write(1,1,'Master',style1)
sheet.write_merge(1,1,2,3,'BW(MB/s)',style1)
sheet.write_merge(1,3,4,4,'iSharkL2\n estimate \nBW(MB/s)',style2)
sheet.write(1,5,'Master',style1)
sheet.write(1,6,'BW(MB/s)',style1)
for x in range(2,4):
	for y in [1,2,3,5,6]:
		sheet.write(x,y,'',style3)
sheet.write(2,1,'GSP',style3)
sheet.write(2,5,'GSP',style3)
sheet.write(3,1,'Total',style3)
sheet.write(3,5,'Total',style3)
book.save('only.xlsx')


執行後的效果:


參考文獻:

http://blog.sina.com.cn/s/blog_5357c0af01019gjo.html

http://codego.net/454548/

http://stackoverflow.com/questions/7746837/python-xlwt-set-custom-background-colour-of-a-cell