Set different font colors for different lines in excel

Tools: 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)    #保存

Perfect: set different background colors for different rows of excel

Continue to improve

Guess you like

Origin blog.csdn.net/weixin_38664232/article/details/107154449