我收到了一份包含每周人数数据的Excel表格
Sam Tony Michael
10 34 25
10 22 25
10 22 33
10 22 33
要求比较各自列中具有第一个值的行,然后为与列中第一行值不匹配的单元格着色
我已经能够编写代码来查找不同的值,但无法为这些单元格着色
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
还不确定是否可以更改相同的输入表单元格颜色或必须创建新单元格
我试过跟随,但是我无法从保存python的格式打开文件:
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)
答案 0 :(得分:0)
终于得到了理想的结果:
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')