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()















原文地址:https://www.cnblogs.com/xiaoluoboer/p/9752867.html