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)

 

posted @ 2022-05-25 15:21  豆浆D  阅读(560)  评论(0编辑  收藏  举报