Python办公自动化教程(008):设置excel单元格边框和背景颜色

3.2 添加边框

示例代码

python 复制代码
import openpyxl
from openpyxl.styles import Border, Side

# 1️⃣ 创建 Excel 工作簿
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = "球员信息"

# 2️⃣ 定义边框样式(细线边框)
thin_border = Border(
    left=Side(style="thin"),
    right=Side(style="thin"),
    top=Side(style="thin"),
    bottom=Side(style="thin"),
)

# 3️⃣ 写入数据
data = [
    ["球员", "球队"],
    ["凯文·杜兰特", "太阳"],
    ["德文·布克", "太阳"],
    ["布拉德利·比尔", "太阳"],
    ["克里斯·保罗", "勇士"],
    ["斯蒂芬·库里", "勇士"]
]

for row_idx, row in enumerate(data, start=1):  # 从 Excel 第 1 行开始
    for col_idx, value in enumerate(row, start=1):  # 从 Excel 第 1 列开始
        cell = sheet.cell(row=row_idx, column=col_idx, value=value)
        cell.border = thin_border  # 应用边框

# 4️⃣ 保存文件
wb.save("suns.xlsx")
wb.close()

实现效果

你可以更改 Side(style="thin") 来调整边框:

  • 细线Side(style="thin")
  • 粗线Side(style="thick")
  • 虚线Side(style="dashed")
  • 点线Side(style="dotted")
  • 双线Side(style="double")

3.3 设置背景颜色

示例代码

python 复制代码
import openpyxl
from openpyxl.styles import PatternFill, Border, Side, Font

# 1️⃣ 创建 Excel 工作簿
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = "球员信息"

# 2️⃣ 定义边框样式(细线边框)
thin_border = Border(
    left=Side(style="thin"),
    right=Side(style="thin"),
    top=Side(style="thin"),
    bottom=Side(style="thin"),
)

# 3️⃣ 定义填充颜色 & 字体样式
header_fill = PatternFill(fill_type="solid", fgColor="ffffff")  # 浅灰色(表头)
suns_fill = PatternFill(fill_type="solid", fgColor="FF8C00")  # 深橙色(太阳队)
warriors_fill = PatternFill(fill_type="solid", fgColor="00008B")  # 深蓝色(勇士队)
default_fill = PatternFill(fill_type="solid", fgColor="333333")  # 深灰色(其他球队)

# **字体样式**
header_font = Font(color="000000", bold=True)  # **表头字体:黑色 + 加粗**
white_font = Font(color="FFFFFF", bold=True)  # **数据字体:白色 + 加粗**

# 4️⃣ 写入数据
data = [
    ["球员", "球队"],  # 表头
    ["凯文·杜兰特", "太阳"],
    ["德文·布克", "太阳"],
    ["布拉德利·比尔", "太阳"],
    ["克里斯·保罗", "马刺"],
    ["斯蒂芬·库里", "勇士"],
    ["扬尼斯·阿德托昆博", "雄鹿"]
]

for row_idx, row in enumerate(data, start=1):  # 从 Excel 第 1 行开始
    # 5️⃣ 确定整行颜色
    if row_idx == 1:
        row_fill = header_fill  # **表头 浅灰色**
        row_font = header_font  # **表头字体 黑色**
    elif row[1] == "太阳":
        row_fill = suns_fill  # **太阳队 深橙色**
        row_font = white_font  # **白色字体**
    elif row[1] == "勇士":
        row_fill = warriors_fill  # **勇士队 深蓝色**
        row_font = white_font  # **白色字体**
    else:
        row_fill = default_fill  # **其他队伍 深灰色**
        row_font = white_font  # **白色字体**

    for col_idx, value in enumerate(row, start=1):  # 从 Excel 第 1 列开始
        cell = sheet.cell(row=row_idx, column=col_idx, value=value)
        cell.border = thin_border  # 应用边框
        cell.fill = row_fill  # 应用整行背景色
        cell.font = row_font  # 应用字体颜色

# 6️⃣ 保存文件
wb.save("nba_teams_header_black.xlsx")
wb.close()

实现效果

颜色自定义

  • 表头背景色 可调整 header_fill = PatternFill(fill_type="solid", fgColor="XXXXXX")
  • 表头字体颜色 header_font = Font(color="000000", bold=True)000000 为黑色)
  • 数据字体颜色 white_font = Font(color="FFFFFF", bold=True)FFFFFF 为白色)
相关推荐
李妍.1 小时前
02Numpy基础(上)
开发语言·python
TheBestRucy1 小时前
Python 九阳神功之贰:面向对象(下)
开发语言·python
2601_965958462 小时前
口腔黏膜脱皮超2周未愈建议及时就医
人工智能·python
微小冷3 小时前
在不同空间中展示数据(欧式、球面、庞加莱圆盘)
python·双曲空间·微分几何·geomstats·庞加莱圆盘·球面
weixin_431600443 小时前
Agent Workflow 学习向:Code 节点,比模板更强的变量加工
后端·python·学习·ai·
讲温控就好了3 小时前
从医疗影像到能源存储——芯片冷却温控技术的跨行业赋能
人工智能·python·能源
DeepVisionary4 小时前
从 Qwen3.8-27B 把原生多模态、原生 FP8、桌面级 Agent 三件事一次集齐,看开源大模型的“工业化拐点“
python·自动化
固定资产管理系统软件5 小时前
商务局RFID固定资产管理系统的应用价值与落地要点解析
大数据·python
TheBestRucy5 小时前
Python 九阳神功:从零筑基到线程飞升
服务器·开发语言·网络·人工智能·python
lancyu6 小时前
关于多轮对话机器人的上下文Token优化和解决方案
人工智能·python·深度学习·机器学习·chatgpt·机器人·prompt