python的EXCEL脚本

作用

对给定的列提取第1行,第27行,2-24行红色填充的数据,替换并生成sql语句,保存为output.txt

代码

要求安装openpyxl库

python 复制代码
pip intall openpyxl
python 复制代码
import openpyxl
### 对给定的列提取第1行,第27行,2-24行红色填充的数据,替换并生成sql语句,保存为output.txt ###

# 定义颜色填充的识别函数
def is_red_fill(cell):
    # 检查单元格是否有填充,并且填充颜色是否为红色(FFFF0000)
    return cell.fill.start_color.index == 'FFFF0000'

# 读取Excel文件并获取指定列的数据,提取第1行,第27行,2-24行红色填充的数
def get_data_from_excel(file_path, column):
    wb = openpyxl.load_workbook(file_path)
    ws = wb.active
    
    # 获取第1行的文本
    first_row_text = ws[column + '1'].value if ws[column + '1'].value else ''
    
    # 获取第27行的文本
    twenty_seventh_row_text = ws[column + '27'].value if ws[column + '27'].value else ''
    
    # 获取第2行到第24行红色填充的数据
    red_filled_texts = []
    for row in range(2, 25):  # 从第2行到第24行
        cell = ws[column + str(row)]
        if is_red_fill(cell) and cell.value:  # 检查是否有红色填充并且单元格有值
            red_filled_texts.append(cell.value)
    
    return first_row_text, twenty_seventh_row_text, red_filled_texts


# 调用函数并打印结果
def fanction(file_path, columns):  
    result = []
    for column in columns:
        # column = 'AE'  # 假设我们关注的是B列
        first_row_text, twenty_seventh_row_text, red_filled_texts = get_data_from_excel(file_path, column)
        red_filled_texts = tuple(red_filled_texts)
        red_filled_texts = str(red_filled_texts)

        # print(f"第1行的文本: {first_row_text}")
        # print(f"第27行的文本: {twenty_seventh_row_text}")
        # print(f"第2行到第24行红色填充的数据: {red_filled_texts}")

        if (first_row_text == "") | (twenty_seventh_row_text == "") | (len(red_filled_texts) < 3):
            print(column, "数据不全")
            continue  # 数据不全跳过

        sql1 = f"""update t_LIMS_ORDERTASK OT 
left join T_LIMS_TEST t on T.ID=OT.TESTID
set T.TESTUSERNAME='{twenty_seventh_row_text}'
where T.TESTREPORTFORMULA='{first_row_text}' and OT.TESTUSERNAME in {red_filled_texts};"""

        sql2 = f"""update T_LIMS_ORDERTASKLOG L 
left join T_LIMS_ORDERTASK L on L.ORDERTASKID =OT.ID
left join T_LIMS_TEST t on T.ID=OT.TESTID 
set L.CREATEDBYNAME ='{twenty_seventh_row_text}'
where T.TESTREPORTFORMULA='{first_row_text}' and L.CREATEDBYNAME  in {red_filled_texts} and L.OPERATIONTYPE ='班长审核';"""

        result.append(sql1)
        result.append(sql2)

    with open('output.txt', 'w', encoding='utf-8') as f:
        for item in result:
            f.write("%s\n\n" % item)
        print("output.txt保存成功!")

path = r"C:\Users\16071\Desktop\LIMS未持证上岗资质异常处理.xlsx"
cols = ['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AE']
fanction(path, cols)
相关推荐
金銀銅鐵2 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup117 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi009 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵11 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf12 小时前
Agent 流程编排
后端·python·agent
copyer_xyf12 小时前
Agent RAG
后端·python·agent
copyer_xyf12 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf12 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python