杰瑞科技汇

Python xlwt如何设置单元格颜色?

Of course! Working with colors in xlwt is a common requirement for creating styled Excel files. The key thing to understand is that xlwt does not use standard color names like "red" or "blue". Instead, it uses a color palette system.

Here’s a complete guide on how to use colors in xlwt.

The Basics: The Color Palette

xlwt has a built-in color palette of 64 colors. To use a color, you must refer to it by its index (an integer from 0 to 63).

You can find a list of these colors and their indexes online by searching for "xlwt color palette". Here are a few common ones for quick reference:

Index Color Name Hex Code
0 Black #000000
1 White #FFFFFF
8 Dark Red #C00000
17 Dark Blue #000080
18 Dark Green #008000
23 Light Orange #FFC000
27 Light Blue #00B0F0
29 Light Yellow #FFFF00
32 Light Red #FF0000
33 Light Green #00FF00
34 Light Cyan #00FFFF
36 Dark Purple #800080
62 Gray 25% #D9D9D9
63 Gray 50% #808080

Applying Colors to Cell Content (Font Color)

To change the color of the text in a cell, you need to create a Font object, set its colour attribute to the desired color index, and then apply that font to a XFStyle object.

Example: Changing Text Color

import xlwt
from xlwt import Workbook, Font, XFStyle
# 1. Create a new workbook and a sheet
wb = Workbook()
ws = wb.add_sheet('Sheet1')
# 2. Define the colors you want to use (by their palette index)
black_index = 0
red_index = 32
green_index = 33
blue_index = 27
# 3. Create Font objects for each color
font_black = Font()
font_black.colour_index = black_index
font_red = Font()
font_red.colour_index = red_index
font_green = Font()
font_green.colour_index = green_index
font_blue = Font()
font_blue.colour_index = blue_index
# 4. Create XFStyle objects and apply the fonts
style_black = XFStyle()
style_black.font = font_black
style_red = XFStyle()
style_red.font = font_red
style_green = XFStyle()
style_green.font = font_green
style_blue = XFStyle()
style_blue.font = font_blue
# 5. Write to the cells using the styles
ws.write(0, 0, 'This is Black Text', style_black)
ws.write(1, 0, 'This is Red Text', style_red)
ws.write(2, 0, 'This is Green Text', style_green)
ws.write(3, 0, 'This is Blue Text', style_blue)
# Save the workbook
wb.save('font_colors.xls')

Applying Colors to Cell Backgrounds

The process for setting a cell's background color is very similar. You create a Pattern object, set its pattern attribute to a solid pattern (xlwt.Pattern.SOLID_PATTERN), and set its back_colour_index to the color index.

Example: Changing Cell Background Color

import xlwt
from xlwt import Workbook, Pattern, XFStyle
# 1. Create a new workbook and a sheet
wb = Workbook()
ws = wb.add_sheet('Sheet1')
# 2. Define the colors you want to use (by their palette index)
yellow_index = 29
light_gray_index = 62
# 3. Create Pattern objects for each background color
pattern_yellow = Pattern()
pattern_yellow.pattern = Pattern.SOLID_PATTERN
pattern_yellow.back_colour_index = yellow_index
pattern_gray = Pattern()
pattern_gray.pattern = Pattern.SOLID_PATTERN
pattern_gray.back_colour_index = light_gray_index
# 4. Create XFStyle objects and apply the patterns
style_yellow_bg = XFStyle()
style_yellow_bg.pattern = pattern_yellow
style_gray_bg = XFStyle()
style_gray_bg.pattern = pattern_gray
# 5. Write to the cells using the styles
ws.write(0, 0, 'Yellow Background', style_yellow_bg)
ws.write(1, 0, 'Light Gray Background', style_gray_bg)
# Save the workbook
wb.save('background_colors.xls')

Combining Colors and Other Styles

A powerful feature of xlwt is that you can combine styles. You can create a single XFStyle object that applies a specific font color, a background color, bold text, and borders all at once.

Example: Combining Font Color, Background, and Bold

import xlwt
from xlwt import Workbook, Font, Pattern, XFStyle, Borders
# 1. Create a new workbook and a sheet
wb = Workbook()
ws = wb.add_sheet('Styled Sheet')
# 2. Create a Font object (bold and red)
font_bold_red = Font()
font_bold_red.bold = True
font_bold_red.colour_index = 32  # Light Red
# 3. Create a Pattern object (yellow background)
pattern_yellow = Pattern()
pattern_yellow.pattern = Pattern.SOLID_PATTERN
pattern_yellow.back_colour_index = 29  # Light Yellow
# 4. Create a Borders object (thin black border)
borders = Borders()
borders.left = Borders.THIN
borders.right = Borders.THIN
borders.top = Borders.THIN
borders.bottom = Borders.THIN
# 5. Create a single XFStyle and apply all components
style_combined = XFStyle()
style_combined.font = font_bold_red
style_combined.pattern = pattern_yellow
style_combined.borders = borders
# 6. Write to the cell using the combined style
ws.write(0, 0, 'Bold Red Text on Yellow with Border', style_combined)
# Save the workbook
wb.save('combined_styles.xls')

Advanced: Adding Custom Colors (The Palette Limitation)

A major limitation of xlwt is that it only supports the 64 colors in its default palette. You cannot add an arbitrary color (like #AABBCC) directly.

Workaround: If you absolutely need a specific color that isn't in the palette, your best option is to find the closest match in the standard palette and use that index. You can find many "color palette to Excel index" converters online.

Summary

Task Key Object to Create Attribute to Set Value Type
Change Text Color Font colour_index Integer (0-63)
Change Cell Background Pattern back_colour_index Integer (0-63)
Combine Styles XFStyle font, pattern, etc. Other style objects
Set Pattern Type (for Pattern) pattern Pattern.SOLID_PATTERN Constant

By following these patterns, you can effectively control the appearance of your text and cells in Excel files generated with xlwt.

分享:
扫描分享到社交APP
上一篇
下一篇