python 设置 win32 excel 设置 非连续单元格 背景色

python 复制代码
import win32com.client as win32

def select_and_color_cells(file_path, sheet_name, search_text):
    # Create a new Excel application
    excel = win32.Dispatch("Excel.Application")

    # Open the workbook
    workbook = excel.Workbooks.Open(file_path)

    # Select the active sheet
    sheet = workbook.Sheets(sheet_name)



    # 获取第一列的范围
    first_column_range = sheet.Range(sheet.Cells(1, 1), sheet.Cells(sheet.UsedRange.Rows.Count, 1))

    # 将第一列的值读取到一个 Python 列表中
    first_column_list = [str(cell.Value) for cell in first_column_range]
    print('first_column_list',first_column_list)

    new_list = []
    for index, value in enumerate(first_column_list):
        if value and search_text in value:
            # 将单元格的背景色设为绿色
            new_list.append(index)


    #
    new_list = [f"A{index + 1}" for index in new_list]
    print('new_list',new_list)

    color = 65535  # 背景色,这里使用颜色的索引,65535代表黄色
    # 将所有要设置背景色的单元格地址拼接成一个字符串
    cell_range = ",".join(new_list)
    # 使用 Range 对象的 Union 方法获取所有要设置背景色的单元格范围
    cells = sheet.Range(cell_range)
    # 设置背景色
    cells.Interior.Color = color



# Example usage
file_path = r"C:\Users\Administrator\Documents\Book1 - 副本.xlsx"
sheet_name = "Sheet1"  # Replace with the name of your sheet
search_text = "11"

select_and_color_cells(file_path, sheet_name, search_text)
python 复制代码
import win32com.client as win32

def set_cell_background_color(file_path, sheet_name, cell_addresses, color):
    excel = win32.gencache.EnsureDispatch("Excel.Application")
    excel.Visible = True

    workbook = excel.Workbooks.Open(file_path)
    sheet = workbook.Worksheets(sheet_name)

    try:
        color = 65535  # 背景色,这里使用颜色的索引,65535代表黄色
        # 将所有要设置背景色的单元格地址拼接成一个字符串
        cell_range = ",".join(cell_addresses)
        # 使用 Range 对象的 Union 方法获取所有要设置背景色的单元格范围
        cells = sheet.Range(cell_range)
        # 设置背景色
        cells.Interior.Color = color
    except Exception as e:
        print(f"Error occurred: {str(e)}")
    finally:
        # workbook.Save()
        # excel.Quit()
        pass

# 使用示例
file_path = r"C:\Users\Administrator\Documents\Book1 - 副本.xlsx"

sheet_name = "Sheet1"
cell_addresses = ["A1", "B3", "C5", "D7"]  # 非连续的单元格地址列表
color = 65535  # 背景色,这里使用颜色的索引,65535代表黄色

set_cell_background_color(file_path, sheet_name, cell_addresses, color)



# 示例用法
file_path = r"C:\Users\Administrator\Documents\Book1 - 副本.xlsx"
sheet_name = "Sheet1"  # 替换为您的工作表名称
search_text = "11"
相关推荐
0wioiw024 分钟前
Python基础(Flask①)
后端·python·flask
芥子沫33 分钟前
Jenkins常见问题及解决方法
windows·https·jenkins
飞翔的佩奇1 小时前
【完整源码+数据集+部署教程】食品分类与实例分割系统源码和数据集:改进yolo11-AggregatedAttention
python·yolo·计算机视觉·数据集·yolo11·食品分类与实例分割
OperateCode1 小时前
AutoVideoMerge:让二刷更沉浸的自动化视频处理脚本工具
python·opencv·ffmpeg
蔡俊锋1 小时前
Javar如何用RabbitMQ订单超时处理
java·python·rabbitmq·ruby
跟橙姐学代码1 小时前
学Python别死记硬背,这份“编程生活化笔记”让你少走三年弯路
前端·python
站大爷IP2 小时前
Python与MySQL:从基础操作到实战技巧的完整指南
python
老歌老听老掉牙2 小时前
SymPy 矩阵到 NumPy 数组的全面转换指南
python·线性代数·矩阵·numpy·sympy
站大爷IP2 小时前
Python条件判断:从基础到进阶的实用指南
python
赛博郎中2 小时前
pygame小游戏飞机大战_8继承精灵玩家优化
python·pygame