• pandas美化excel高亮某行


    代码

    from pathlib import Path
    import pandas as pd
    import datetime
    
    # 精简报告内容
    today = datetime.date.today()
    df = pd.read_excel('/Users/soymilk/Documents/records.xlsx')
    
    df_beautiful = pd.DataFrame()
    df_beautiful['功能点'] = df['NUMBER']
    df_beautiful['功能名称'] = df['NAME']
    df_beautiful['访问链接'] = df['URL']
    df_beautiful['点检结果'] = df['RESULT']
    
    
    report = pd.DataFrame({
        '功能点': ['点检报告'],
        '功能名称': [today],
        '访问链接': ['王军武'],
        '点检结果': ['15900729153']
    })
    
    # 美化点检结果这列
    df_beautiful['点检结果'] = df_beautiful['点检结果'].fillna('通过')
    df_beautiful.loc[df_beautiful['点检结果'] == 'pass', '点检结果'] = '通过'
    df_beautiful['点检结果'] = df_beautiful['点检结果'].apply(
        lambda v: '通过' if v == '通过' else '不通过')
    
    
    # 添加一行
    df_beautiful = pd.concat([df_beautiful, report], ignore_index=True)
    
    # 美化最后一行
    
    
    def background_color_negative(v, color):
        return f"background-color: {color};"
    
    
    max_index = list(df_beautiful.index)[-1]
    df_beautiful = df_beautiful.style.applymap(background_color_negative, color='yellow',
                                               subset=(slice(max_index, max_index, 1)))
    df_beautiful.to_excel(
        '/Users/soymilk/Documents/df_beautiful2.xlsx', index=False)
  • 相关阅读:
    递归分治策略
    矩阵连乘问题
    棋盘覆盖问题
    选择排序
    Dijkstra的双栈算术表达式求值算法
    斐波那契数列
    二分算法
    Linux服务器上tengine的安装配置
    Excel Sheet Column Number
    Excel Sheet Column Title
  • 原文地址:https://www.cnblogs.com/soymilk2019/p/16309424.html
Copyright © 2020-2023  润新知