【已解决】Python中使用xlwt设置cell的背景色
【问题】
想要使用Python的xlwt设置单元格的背景色。
但是不知道如何设置。
【解决过程】
1.从Working with Excel Files in Python 找到官网的:The xlwt Module,无果。
2.参考:
XLWT : Changing the Cell Background Color
试了其代码:
|
1
2
3
4
5
6
7
8
9
10
|
badBG = xlwt.Pattern()badBG.SOLID_PATTERN = 0x34badBG.NO_PATTERN = 0x34badBG.pattern_fore_colour = 0x34badBG.pattern_back_colour = 0x34badFontStyle = xlwt.XFStyle()badFontStyle.Pattern = badBGsheet1.write(1,1,'hello world', badFontStyle) |
未果。
不过后来才知道,下面的代码:
|
1
2
3
4
5
6
|
badBG = xlwt.Pattern()badBG.pattern = badBG.SOLID_PATTERNbadBG.pattern_fore_colour = 3badFontStyle = xlwt.XFStyle()badFontStyle.pattern = badBG |
是可以设置背景色的。
但是很有限制。
而且对应的3是Green的意思,也是后来才知道的。
3.后来
是从
python xlwt set custom background colour of a cell
中,试了试其所用的代码:
|
1
|
style1 = xlwt.easyxf('pattern: pattern solid, fore_colour red;') |
最终,经过测试,改为自己所需的代码的:
|
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
|
import xlwt; #styleBlueBkg = xlwt.easyxf('font: color-index red, bold on'); #styleBlueBkg = xlwt.easyxf('font: background-color-index red, bold on'); #styleBlueBkg = xlwt.easyxf('pattern: pattern solid, fore_colour red;'); #styleBlueBkg = xlwt.easyxf('pattern: pattern solid, fore_colour blue;'); #styleBlueBkg = xlwt.easyxf('pattern: pattern solid, fore_colour light_blue;'); #styleBlueBkg = xlwt.easyxf('pattern: pattern solid, fore_colour pale_blue;'); #styleBlueBkg = xlwt.easyxf('pattern: pattern solid, fore_colour dark_blue;'); #styleBlueBkg = xlwt.easyxf('pattern: pattern solid, fore_colour dark_blue_ega;'); #styleBlueBkg = xlwt.easyxf('pattern: pattern solid, fore_colour ice_blue;'); styleBlueBkg = xlwt.easyxf('pattern: pattern solid, fore_colour ocean_blue; font: bold on;'); # 80% like #styleBlueBkg = xlwt.easyxf('pattern: pattern solid, fore_colour sky_blue;'); #blueBkgFontStyle = xlwt.XFStyle() #blueBkgFontStyle.Pattern = blueBackgroundPattern; #styleBlueBkg = blueBkgFontStyle; styleBold = xlwt.easyxf('font: bold on'); wb = xlwt.Workbook(); ws = wb.add_sheet('realPropertyInfo'); ws.write(0, 0, "Sequence", styleBlueBkg); ws.write(0, 1, "MapID", styleBlueBkg); ws.write(0, 2, "Owner1", styleBold); ws.write(0, 3, "Owner2", styleBold); wb.save(excelFilename); |
对应的,上述颜色的选择,可以参考这里:
https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/Style.py
摘录如下:
|
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
|
# Text values for colour indices. "grey" is a synonym of "gray".# The names are those given by Microsoft Excel 2003 to the colours# in the default palette. There is no great correspondence with# any W3C name-to-RGB mapping._colour_map_text = """\aqua 0x31black 0x08blue 0x0Cblue_gray 0x36bright_green 0x0Bbrown 0x3Ccoral 0x1Dcyan_ega 0x0Fdark_blue 0x12dark_blue_ega 0x12dark_green 0x3Adark_green_ega 0x11dark_purple 0x1Cdark_red 0x10dark_red_ega 0x10dark_teal 0x38dark_yellow 0x13gold 0x33gray_ega 0x17gray25 0x16gray40 0x37gray50 0x17gray80 0x3Fgreen 0x11ice_blue 0x1Findigo 0x3Eivory 0x1Alavender 0x2Elight_blue 0x30light_green 0x2Alight_orange 0x34light_turquoise 0x29light_yellow 0x2Blime 0x32magenta_ega 0x0Eocean_blue 0x1Eolive_ega 0x13olive_green 0x3Borange 0x35pale_blue 0x2Cperiwinkle 0x18pink 0x0Eplum 0x3Dpurple_ega 0x14red 0x0Arose 0x2Dsea_green 0x39silver_ega 0x16sky_blue 0x28tan 0x2Fteal 0x15teal_ega 0x15turquoise 0x0Fviolet 0x14white 0x09yellow 0x0D""" |
4.后来才发现,其实,上述的方法,就是前面在
Setting the Background Color of a Cell
所介绍的内容:
|
1
2
3
4
5
6
7
8
9
10
|
import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')pattern = xlwt.Pattern() # Create the Patternpattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12pattern.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...style = xlwt.XFStyle() # Create the Patternstyle.pattern = pattern # Add Pattern to Styleworksheet.write(0, 0, 'Cell Contents', style)workbook.save('Excel_Workbook.xls') |
只是当时觉得太繁琐,没有自己看而已。
【总结】
如上代码所示:
- 可以通过xlwt.Pattern()然后得到pattern,设置pattern_fore_colour即可,但是颜色选择很有限。
- 也可以通过更方便的:
- xlwt.easyxf(‘pattern: pattern solid, fore_colour ocean_blue; font: bold on;’);
- 去设置背景色。
浙公网安备 33010602011771号