简体   繁体   中英

Changing row background using xlsxwriter

I have an excel wit n rows and 10 columns. To change background of a particular row , I used :

writer = pd.ExcelWriter('pandasEx.xlsx',  engine ='xlsxwriter') 
# Convert the dataframe to an XlsxWriter Excel object
df.to_excel(writer, index=False)
# Get the xlsxwriter objects from the dataframe writer object.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

cell_format = workbook.add_format()
cell_format.set_bg_color('green')
worksheet.set_row(rowNo, cell_format=cell_format)

but the formatting exceeds the 10 columns and goes beyond them. How to keep the formatting limited to 10 columns? Or in general, How to keep the formatting limited to columns which are used?

PS : I found out about conditional_format but i dont understand which type value should i choose?

Below code works for my case. This changes the background without any criteria.

worksheet.conditional_format(row,0,row,lastCol, {'type':'no_blanks', 'format': cell_format})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM