This article mainly for you in detail the use of Python to draw a report to set the cell color of the instance code, with a certain reference value, interested in small partners can refer to
Original address: Python sets cell shading and background color by pattern Author: Tengpython When you set the cell shading and background color of an Excel file by using the pattern, you cannot get the value that you want to set for the cell color, and you start with a stupid method. Starting from 0 to constantly test the corresponding color, the last colleague tip than using Python to write a small program, the 0-256 corresponding color display, so that you can find the color according to their own values. Here to share this small program with you, there is no need to try one by one. If you repeat the operation on a cell, it will throw returns error:# exception:attempt to overwritecell:# sheetname=u ' sheet 1 ' rowx=0colx=0 so add cell_ when opening Overwrite_ok=true resolve the following as source code: #-*-coding:utf-8-*-import xlwt# Create a new Excel file FILE=XLWT. Workbook () #新建一个sheettable =file.add_sheet (' SheetName ', cell_overwrite_ok=true) for I in Range (0,256): stylei= XLWT. Xfstyle () #初始化样式 patterni= XLWT. Pattern () #为样式创建图案 patterni.pattern=1 #设置底纹的图案索引, 1 is solid, 2 is 50% gray, corresponds to the Excel file in cell format in the fill The pattern style patterni.pattern_fore_colour=i #设置底纹的前景色, corresponding to the background color in the fill in the Excel file cell format Patterni.pattern_bac K_colour=35 #设置底纹的背景色 that corresponds to the pattern color in the fill in the Excel file cell format Stylei.pattern=patterni #为样式设置图案 TABLE.WR ITE (I,0,i,stylei) #使用样式file. Save (' D:/python27/colour.xls ')