简体   繁体   中英

Reading colours of cell in an excel sheet using python: openpyxl or any other library

Ok so here is what i need help with, i have an excel sheet and i have used the pattern fill as can be seen in the image below. So what i want to do in the python script is basically to read every row to check if the cell has a pattern fill and if it does use the values found in those particular cells to do something. I have already managed to do the first part which is to read the excel sheet using openpyxl.

patternfill

The sample of the code i have done thus far looks like this. I just need to fill up the step3: if () section if i need to import another library is fine just let me know what i need to do :)

    from openpyxl import load_workbook  

    #Step 3: Use this function to read the colours and do something
    def postDialog(A, B, C, D, E, F, G, H, I, J, K, L):
        if (A == redcolorfill): #HERE How to read pattern fill color of cell

    #Step 1: Execute open excel workbook 
    ACTION1_File = load_workbook('Myfile.xlsx', )
    ACTION1_File_Sheet = ACTION1_File.get_sheet_names()[1]  
    ACTION1_File_Sheet_Name = ACTION1_File.get_sheet_by_name(ACTION1_File_Sheet)

    #Step 2: For every row read columns A-L and use in function postDialog
    for line in ACTION1_File_Sheet_Name.iter_rows():
        #Columns A - L
        postDialog(line[0].value, line[1].value, line[2].value, line[3].value, line[4].value, line[5].value, 
                   line[6].value, line[7].value, line[8].value, line[9].value, line[10].value, line[11].value)  

Here is how you can read the fill color of the cells

for line in ACTION1_File_Sheet_Name.iter_rows():
    for c in line:
        print(c.fill.bgColor)

EDIT: How to read a specific range

for c in ACTION1_File_Sheet_Name["A1:A10"]:
   print(c[0].fill.fgColor)

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