【已解决】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 |
|
未果。
不过后来才知道,下面的代码:
1 2 3 4 5 6 |
|
是可以设置背景色的。
但是很有限制。
而且对应的3是Green的意思,也是后来才知道的。
3.后来
是从
python xlwt set custom background colour of a cell
中,试了试其所用的代码:
1 |
|
最终,经过测试,改为自己所需的代码的:
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 |
|
对应的,上述颜色的选择,可以参考这里:
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 |
|
4.后来才发现,其实,上述的方法,就是前面在
Setting the Background Color of a Cell
所介绍的内容:
1 2 3 4 5 6 7 8 9 10 |
|
只是当时觉得太繁琐,没有自己看而已。
【总结】
如上代码所示:
- 可以通过xlwt.Pattern()然后得到pattern,设置pattern_fore_colour即可,但是颜色选择很有限。
- 也可以通过更方便的:
- xlwt.easyxf(‘pattern: pattern solid, fore_colour ocean_blue; font: bold on;’);
- 去设置背景色。