from openpyxl import load_workbook
from openpyxl.styles import PatternFill
def Modify_background_color(workbook_path,sheet_name,cell_range,rgb_value):
# 加载现有的工作簿
wb = load_workbook(workbook_path)
# 获取名为 "员工信息" 的工作表
ws = wb[sheet_name]
# 将RGB转换为ARGB格式,用于openpyxl
argb_value = "#{:02x}{:02x}{:02x}".format(*rgb_value)
print(argb_value)
# 创建填充样式
fill = PatternFill(start_color=argb_value[1:], end_color=argb_value[1:], fill_type="solid")
# 应用填充样式到单元格
ws[cell_range].fill = fill
# 保存工作簿
wb.save(workbook_path)
Modify_background_color("D:\\desktop\\测试文件夹\\Workbook.xlsx","员工信息","A1",(0, 100, 0))
python_根据RGB值填充excel背景色
于 2024-08-19 19:26:45 首次发布