Excel 表格汇总及颜色填充

表格汇总

import xlwt
import xlrd
from xlwt import Workbook
import xlwings as xw
from openpyxl import load_workbook
import openpyxl.styles as sty


#汇总表格的整体思路为,创建表格-->读取需要汇总的数据-->将数据写入汇总的表格并保存

# xlwt新建表格


def new_sheet():
    book = Workbook(encoding='utf-8')
    sheet1 = book.add_sheet('Sheet 1')
    # sheet1.write(0, 0, "我是第一行第一列")   这几行为实验代码,测试代码功能
    # sheet1.write(0, 1, "我是第一行第二列")
    # sheet1.write(1, 0, "我是第2行第一列")
    # sheet1.write(1, 1, "我是第2行第二列")
    book.save(path='新建表格路径')




# xlrd获取数据

def get_data():
    data_list = []
    for x in range(1, 6):
        table = xlrd.open_workbook(path='需要填充数据的表格路径')
        sheet = table.sheets()[0]
        rows = sheet.nrows
        # 表格列数
        # cols = sheet.ncols
        for y in range(rows):
            data_list.append(sheet.row_values(y))
    return data_list



# xlwings写入数据

def write_data():
    a = 1
    for x in get_data():
        wb = xw.Book(path='汇总的表路径')
        sht = wb.sheets[0]
        sht.range('A{0}'.format(a)).options(index=False).value = x
        wb.save()
        a += 1

excel 表格填充颜色的功能块

workbook = Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('Sheet1')
pattern = xlwt.Pattern()  # 创建样式
pattern.pattern = xlwt.Pattern.SOLID_PATTERN  # 样式格式
 # 样式颜色: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 =                                   Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta,                            21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
pattern.pattern_fore_colour = 5 
style = xlwt.XFStyle()  # 创建样式
style.pattern = pattern  # 将样式添加到风格中