Openpyxl - checking if cell has fill

最后都变了- 提交于 2021-01-28 11:33:56

问题


I am trying to check if a cell is highlighted in yellow. All the posts I've come across is to fill a cell, not check if it has a fill. Heres my code so far:

coordinates = []

fl = PatternFill(patternType = "solid", fgColor="FFFFFF00", bgColor="FFFFFF00") 
print (fl)

for d in ws['A']:

    if str(d.value)[0:10] == str(last_day_of_month) and d.fill == fl:  
        coordinates.append(d.coordinate)
    elif str(d.value)[0:10] == str(previous_month) and d.fill == fl:
        coordinates.append(d.coordinate)
        break

I don't need to check if the cell is the correct color or not, I just need to know if its highlighted so any method to find out if the cell has a fill would be great.


回答1:


Try this:

if (d.font.color):
    #it's highlighted

Or there is another option:

d.fill.start_color.index

Hope this helps.



来源:https://stackoverflow.com/questions/41256943/openpyxl-checking-if-cell-has-fill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!