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"
相关推荐
一条咸鱼¥¥¥3 分钟前
【运维经验】服务器磁盘做镜像的方法
运维·服务器·windows·经验分享
cpp_learners11 分钟前
Qt Windows版本的《智能鼠标点击器》
windows·qt·鼠标点击器
love530love27 分钟前
Windows 11 下 Z-Image-Turbo 完整部署与 Flash Attention 2.8.3 本地编译复盘
人工智能·windows·python·aigc·flash-attn·z-image·cuda加速
MediaTea1 小时前
Python:模块 __dict__ 详解
开发语言·前端·数据库·python
jarreyer1 小时前
python,numpy,pandas和matplotlib版本对应关系
python·numpy·pandas
代码or搬砖2 小时前
HashMap源码
开发语言·python·哈希算法
顽强卖力3 小时前
第二章:什么是数据分析师?
笔记·python·职场和发展·学习方法
站大爷IP3 小时前
Python实现Excel数据自动化处理:从繁琐操作到智能流程的蜕变
python
BBB努力学习程序设计4 小时前
Python 进阶知识点精讲:上下文管理器(Context Manager)的原理与实战
python·pycharm
清水白石0084 小时前
《深入 super() 的世界:MRO 与 C3 线性化算法的全景解析与实战指南》
python