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"
相关推荐
ITHAOGE151 小时前
下载 | Win10 2021精简版,预装应用极少!(7月更新、Win 10 IoT LTSC 2021版、适合老电脑安装)
windows·物联网·microsoft·微软·电脑
不讲废话的小白2 小时前
给 Excel 整列空格文字内容加上前缀:像给文字穿衣服一样简单!
c语言·excel
橡晟7 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)
人工智能·python·深度学习·机器学习·计算机视觉
墨尘游子7 小时前
神经网络的层与块
人工智能·python·深度学习·机器学习
倔强青铜38 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
企鹅与蟒蛇8 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
autobaba8 小时前
编写bat文件自动打开chrome浏览器,并通过selenium抓取浏览器操作chrome
chrome·python·selenium·rpa
Rvelamen9 小时前
LLM-SECURITY-PROMPTS大模型提示词攻击测评基准
人工智能·python·安全
liulilittle9 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程
【本人】10 小时前
Django基础(一)———创建与启动
后端·python·django