KOTA ITO

Generation

generate functionThu, 15 Dec 2022

openpyxlを使って、Excelファイルの3列目に含まれる日付で行をグルーピングして、日付ごとに新しいシートに格納する。

from openpyxl import load_workbook, Workbook wb = load_workbook('output.xlsx') ws = wb.active def add_sheet(wb, header, data): ws = wb.create_sheet(header) ws.append(['日付', '商品名', '数量']) for row in data: ws.append(row) def group_data(ws): data = {} for row in ws.rows: if row[0].value == '日付': continue row_data = [cell.value for cell in row] if row[0].value not in data: data[row[0].value] = [row_data] else: data[row[0].value].append(row_data) return data data = group_data(ws) for header, rows in data.items(): add_sheet(wb, header, rows

Questions about programming?Chat with your personal AI assistant