简体   繁体   English

使用 xlsxwriter 更改行背景

[英]Changing row background using xlsxwriter

I have an excel wit n rows and 10 columns.我有一个有 n 行 10 列的 excel。 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.但是格式超过了 10 列并且超出了它们。 How to keep the formatting limited to 10 columns?如何将格式限制为 10 列? 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? PS:我发现了conditional_format,但我不明白我应该选择哪种type值?

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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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