Python实操标记Excel的重复内容

from openpyxl import load_workbook, Workbook
from openpyxl.styles import Font, Alignment, colors


def cteate():
    file_path = 'E:\Python/Poject1/2.xlsx'
    wb = load_workbook(file_path)
    sh = wb['电影2']
    font1 = Font(name='微软雅黑',
                 bold=True, color="FF0000")
    t_wb = Workbook()
    w_sh = t_wb.active
    data = []
    re_data = []
    for r in range(1, sh.max_row+1):
        row = []
        for c in range(1, sh.max_column+1):
            if sh.cell(r, c).value in data:
                # sh.cell(r, c).font = font1
                re_data.append(sh.cell(r, c).value)
                row.append(sh.cell(r, c).value)
            else:
                data.append(sh.cell(r, c).value)
                row.append(sh.cell(r, c).value)
        w_sh.append(row)
    print(re_data)

    for r in range(1, w_sh.max_row+1):
        for c in range(1, w_sh.max_column):
            if w_sh.cell(r, c).value in re_data:
                w_sh.cell(r, c).font = font1
                w_sh.cell(r, c).alignment = Alignment(
                    horizontal='center', vertical='center')
    t_wb.save('重复数据.xlsx')


cteate()

你可能感兴趣的:(Python,python,excel,前端)