1. 程式人生 > 其它 >pandas美化excel高亮某行

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)