2.8 查询excel重复数据

from openpyxl import load_workbook
from openpyxl.styles import PatternFill

def dum():
    wb = load_workbook('./办公自动化/files/打卡时间.xlsx')
    sh = wb['Sheet']

    dum_sh = wb.create_sheet('重复数据')

    # 存储哪一行是重复数据
    index = []
    tmp = []   # 没有重复的数据
    print(sh['B'])
    for i,c in enumerate(sh['B']):    # i 从0开始
        # print(i,c.value)
        if c.value not in tmp:
            tmp.append(c.value)
        else:
            index.append(i)

    # print(tmp)
    # print(index)

    fill = PatternFill('solid', fgColor="a1a3a6")
    for i,r in enumerate(sh.rows):   # i 从0开始  如果使用sh[i]获取正确的行数据使用sh[i + 1]
        if i in index:
            dum_list = []  # 重复的数据
            for c in r:
                # dum_sh[c.coordinate].value = c.value    # 复制行数据,但会有空行保留
                dum_list.append(c.value)
                c.fill = fill
            dum_list[0] = dum_list[0].date()
            dum_sh.append(dum_list)

    wb.save('./办公自动化/files/打卡时间.xlsx')

if __name__ == "__main__":
    dum()