Color specific excel cells based on match conditio

2019-09-14 01:34发布

I got an excel sheet with weekly data for number of people

Sam    Tony   Michael 
10      34      25
10      22      25
10      22      33
10      22      33

Requirement is to compare the rows with first value in their respective column and then color the cells which do not match the first row value in their columns

I have been able to write code to find different values, however unable to color those cells

import xlrd
import xlwt
from xlwt import Workbook
from xlrd import open_workbook
wb = open_workbook("test1.xlsx")
mysheet = wb.sheet_by_name('Sheet1')
numrows = mysheet.nrows
value = ""
value1 = ""

col = 0
st = xlwt.easyxf('pattern: pattern solid;')
st.pattern.pattern_fore_colour = 1
while col < 200:
        value = mysheet.cell(1 , col)
        for y in range(numrows-1):
                y +=1
                value1 = mysheet.cell(y , col)
                value = str(value)
                value1 = str(value1)

                if (value1 != value):
                        <Colour the cell>  <<<<<<<<<<<<<<<<<<<<<
                else:
                        continue
        col +=1

Also not sure if the same input sheet's cell colors can be changed or have to create a new one

EDIT 1:

I tried following, however I am unable to open the file from the format in which python is saving:

import xlrd
import xlwt
from xlutils.copy import copy
from xlwt import Workbook
from xlrd import open_workbook
file_path = '/home/zoro/test1.xlsx'
rb = open_workbook(file_path, formatting_info=True)
mysheet = rb.sheet_by_name('Sheet1')
numrows = mysheet.nrows
numcols = mysheet.ncols
value = ""
value1 = ""
z = 1
col = 0
wb = copy(rb)
w_sheet = wb.get_sheet(0)
st = xlwt.easyxf('pattern: pattern solid;')
st.pattern.pattern_fore_colour = 1
while col < numcols:
        value = mysheet.cell(z , col)
        for y in range(numrows-1):
                y +=1
                value1 = mysheet.cell(y , col)
                value = str(value)
                value1 = str(value1)

                if (value1 != value):
                        w_sheet.write(y,col,value1,st)
                else:
                        continue
        col +=1

wb.save(file_path)

1条回答
欢心
2楼-- · 2019-09-14 01:48

Finally got the desired result:

import xlrd
import xlwt
from xlutils.copy import copy
from xlwt import Workbook
from xlrd import open_workbook
rb = open_workbook('test1.xlsx')
mysheet = rb.sheet_by_name('Sheet1')
numrows = mysheet.nrows
numcols = mysheet.ncols
value = ""
value1 = ""
col = 0
wb = copy(rb)
w_sheet = wb.get_sheet(0)
st = xlwt.easyxf('pattern: pattern solid;')
st.pattern.pattern_fore_colour = 4
while col < numcols:
        value = mysheet.cell(1 , col)
        for y in range(numrows-1):
                y +=1
                value1 = mysheet.cell(y , col)
                value = str(value)
                value1 = str(value1)

                if (value1 != value):
                        w_sheet.write(y,col,value1,st)
                else:
                        continue
        col +=1

wb.save('output.xls')
查看更多
登录 后发表回答