写excel带颜色

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

# 写第一行
sheet1.write(0, 0, 'cmsid')
sheet1.write(0, 1, 'title')
sheet1.write(0, 2, 'url')
for idx in range(1, 21):
    if idx == 1:
        tag_name = 'tag{}(带颜色是分类、不带颜色是标签)'.format(idx)
    else:
        tag_name = 'tag{}'.format(idx)
    sheet1.write(0, idx+2, tag_name)

style1 = xlwt.easyxf('pattern: pattern solid, fore_colour green;')
with open('./color_demo.txt', encoding='utf-8') as fr:
    data_ = fr.readlines()
    data = data_[1:]
    for cid, line in enumerate(data):
        cid += 1
        cmsid = line.strip().split('\t')[0]
        title = line.strip().split('\t')[1]
        url = line.strip().split('\t')[2]
        # url = 'https://view.inews.qq.com/a/{}'.format(cmsid)

        sheet1.write(cid, 0, cmsid)
        sheet1.write(cid, 1, title)
        sheet1.write(cid, 2, url)
        new_cate_list = line.strip().split('\t')[3:]
        sheet_col = 2
        tag_src_str, only_tag_src = '', ''
        for cate in new_cate_list:
            # tag_src_str += '\t' + cate + '\tcate'
            # only_tag_src += '\t' + cate
            sheet_col += 1
            sheet1.write(cid, sheet_col, cate, style1)
        sheet_col_n = sheet_col
        for cate1 in new_cate_list:
            sheet_col_n += 1
            sheet1.write(cid, sheet_col_n, cate1)

book.save('moving_average4.xls')

你可能感兴趣的:(写excel带颜色)