跳转至

颜色和填充

本页面介绍如何使用颜色和填充样式。

颜色表示

创建颜色

from symphra_excel.styles import Color

color1 = Color.from_hex("#FF0000")

color2 = Color(rgb="FF0000")

color3 = Color(theme=1)

背景颜色

设置单元格背景色

from symphra_excel import Workbook
from symphra_excel.styles import CellStyle

wb = Workbook()
sheet = wb.create_worksheet("颜色示例")

style = CellStyle()
style.set_background_color("#FFFF00")
sheet.set_cell_value("A1", "黄色背景")
sheet.apply_style("A1", style)

填充模式

实心填充

style = CellStyle()
style.set_fill(pattern="solid", fg_color="#FF0000")
sheet.set_cell_value("A2", "红色实心")
sheet.apply_style("A2", style)

图案填充

style = CellStyle()
style.set_fill(
    pattern="gray125",
    fg_color="#000000",
    bg_color="#FFFFFF"
)
sheet.set_cell_value("A3", "灰色图案")
sheet.apply_style("A3", style)

常用颜色

基本颜色

colors = {
    "红色": "#FF0000",
    "绿色": "#00FF00",
    "蓝色": "#0000FF",
    "黄色": "#FFFF00",
    "橙色": "#FFA500",
    "紫色": "#800080",
}

row = 1
for name, hex_color in colors.items():
    style = CellStyle()
    style.set_background_color(hex_color)
    sheet.set_cell_value(f"A{row}", name)
    sheet.apply_style(f"A{row}", style)
    row += 1

完整示例

示例1: 彩虹色带

from symphra_excel import Workbook
from symphra_excel.styles import CellStyle

with Workbook() as wb:
    sheet = wb.create_worksheet("彩虹")

    rainbow = [
        ("#FF0000", "红"),
        ("#FFA500", "橙"),
        ("#FFFF00", "黄"),
        ("#00FF00", "绿"),
        ("#0000FF", "蓝"),
        ("#4B0082", "靛"),
        ("#8B00FF", "紫"),
    ]

    for idx, (color, name) in enumerate(rainbow, start=1):
        style = CellStyle()
        style.set_background_color(color)
        style.set_font_color("#FFFFFF")
        sheet.set_cell_value(f"A{idx}", name)
        sheet.apply_style(f"A{idx}", style)

    wb.save("rainbow.xlsx")

示例2: 状态标识

from symphra_excel import Workbook
from symphra_excel.styles import CellStyle

with Workbook() as wb:
    sheet = wb.create_worksheet("状态")

    success_style = CellStyle()
    success_style.set_background_color("#D4EDDA")
    success_style.set_font_color("#155724")

    warning_style = CellStyle()
    warning_style.set_background_color("#FFF3CD")
    warning_style.set_font_color("#856404")

    error_style = CellStyle()
    error_style.set_background_color("#F8D7DA")
    error_style.set_font_color("#721C24")

    sheet.set_cell_value("A1", "成功")
    sheet.apply_style("A1", success_style)

    sheet.set_cell_value("A2", "警告")
    sheet.apply_style("A2", warning_style)

    sheet.set_cell_value("A3", "错误")
    sheet.apply_style("A3", error_style)

    wb.save("status.xlsx")

填充图案类型

可用图案

patterns = [
    "solid",
    "gray125",
    "darkGray",
    "mediumGray",
    "lightGray",
]

for idx, pattern in enumerate(patterns, start=1):
    style = CellStyle()
    style.set_fill(pattern=pattern, fg_color="#000000")
    sheet.set_cell_value(f"A{idx}", pattern)
    sheet.apply_style(f"A{idx}", style)

最佳实践

✅ 推荐做法

style = CellStyle()
style.set_background_color("#E8F5E9")

color_map = {
    "success": "#D4EDDA",
    "warning": "#FFF3CD",
    "error": "#F8D7DA",
}
for status, color in color_map.items():
    style = CellStyle()
    style.set_background_color(color)

❌ 避免的做法

style = CellStyle()
style.set_background_color("#ZZZZZZ")

for i in range(10000):
    style = CellStyle()
    style.set_background_color("#FF0000")
    sheet.apply_style(f"A{i}", style)

下一步