如何用颜色将列表写入Excel文件

时间:2019-06-18 08:01:15

标签: python pandas

我有一个类似清单的清单

x=[['My??50','Name??70','is??90'],['this??90','is??80','another??40','line??70'],['lets??90','take??90','another??90','line??70']]

我想将此写入excel / csv文件。条件是我要写在“ ??”之前的文本仅在excel文件中。 '??'后面的数字将用于检查单元是否应着色。如果数字后面有'??'小于90,我想将其着色为红色。 例如

text='My??50'.split('??')[0]
number=int('My??50'.split('??')[-1])
if number<90:
   write(text with colour red)

我试图在熊猫中使用Styler,但没有锻炼。

1 个答案:

答案 0 :(得分:0)

我能够解决这个问题,

from xlwt import Workbook
import xlwt
book = Workbook()
sheet1 = book.add_sheet('Sheet 1')

x=[['My??50','Name??70','is??90'],['this??90','is??80','another??40','line??70'],
   ['lets??90','take??90','another??90','line??70']]


st = xlwt.easyxf('pattern: pattern solid;')
st.pattern.pattern_fore_colour = 20
for i, l in enumerate(x):
    for j, col in enumerate(l):
        print(col)
        if int(col.split('??')[-1])<85:
            sheet1.write(i, j, col.split('??')[0],st)
        else:
            sheet1.write(i, j, col.split('??')[0])
book.save('C:\\Users\\sandeep.sharma\\Desktop\\vishal\\data\\simple.xls')