excel中的不同行設置不同的字體顏色

工具:xlwt

import xlwt
import pandas as pd

df_data=pd.read_excel('total_data2.xlsx')
filename=(u'熱門.xlsx') #要保存的文件路徑

wb = xlwt.Workbook()
sheet = wb.add_sheet('sheet1')
style = "font:colour_index orange;"    #設置字體顏色
yellow_style = xlwt.easyxf(style)

hot_req=['補貼','職業技能',]

columns_list=df_data.columns

hot_count=0
question_list=[]
for index in df_data.index:
    is_high=None
    question=str(df_data['問題'][index])
    if not pd.isna(question):
        for i in hot_req:
            if i in question:
                is_high = True
                question_list.append(question)
        for col_index in range(len(columns_list)):
            if index==0:
                sheet.write(index,col_index,columns_list[col_index])    #字體不做改變
            else:
                if is_high:
                    sheet.write(index, col_index, str(df_data[columns_list[col_index]][index])
                                , yellow_style)    #設置橘黃色字體
                    hot_count+=1
                else:
                    sheet.write(index, col_index, str(df_data[columns_list[col_index]][index]))


wb.save(filename)    #保存

完善:爲excel不同行設置不同背景色

後續繼續完善

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章