1.4 表格的数据拆分

import xlrd
from xlutils.copy import copy

def get_data():
    wb = xlrd.open_workbook('./办公自动化/files/orders.xls')
    sh = wb.sheet_by_index(0)
    all_data = {}
    for r in range(1, sh.nrows):
        d = {'type': sh.cell_value(r, 1), 'name': sh.cell_value(
            r, 2), 'count': sh.cell_value(r, 3), 'price': sh.cell_value(r, 4)}
        key = sh.cell_value(r, 0)
        if all_data.get(key):
            all_data[key].append(d)
        else:
            all_data[key] = [d]
    # print(all_data)
    return all_data

def save(data):
    wb = xlrd.open_workbook('./办公自动化/files/orders.xls')
    wb2 = copy(wb)

    for key in data.keys():
        temp_sheet = wb2.add_sheet(key)
        for i,d in enumerate(data.get(key)):
            temp_sheet.write(i,0,d.get('type'))
            temp_sheet.write(i,1,d.get('name'))
            temp_sheet.write(i,2,d.get('count'))
            temp_sheet.write(i,3,d.get('price'))

    wb2.save('./办公自动化/files/orders.xls')

if __name__ == '__main__':
    data = get_data()
    save(data)