1. 程式人生 > >18-10-08 對Excel 表格新增顏色

18-10-08 對Excel 表格新增顏色

#-*- coding:utf-8 -*-
#首先注意一個問題 pycharm 中路徑符號和Linux 系統一樣向左
import xlwt

def add_color():
#例項化物件 增加sheet 建立模式 綠色三行 紅色三行
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('我的 Sheet1')
pattern1 = xlwt.Pattern() # Create the Pattern 模式
pattern1.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern1.pattern_fore_colour = 3 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
style1 = xlwt.XFStyle() # Create the Pattern 樣式
style1.pattern = pattern1 # Add Pattern to Style
worksheet.write(0, 0, '第一行第一列', style1)
worksheet.write(1, 0, '第二行第一列', style1)
worksheet.write(2, 0, '第三行第一列', style1)

pattern2 = xlwt.Pattern() # Create the Pattern 模式
pattern2.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern2.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
style2 = xlwt.XFStyle() # Create the Pattern 樣式

style2.pattern = pattern2 # Add Pattern to Style
worksheet.write(0, 1, '第一行第二列', style2)
worksheet.write(1, 1, '第二行第二列', style2)
worksheet.write(2, 1, '第三行第二列', style2)


workbook.save('C:/Users/WY/Desktop/aaa.xls')

if __name__ == "__main__":
add_color()