KOTA ITO

Generation

generate functionThu, 15 Dec 2022

openpyxlを使ってExcelファイルを開き、シートの3列目に含まれる日付でデータをグルーピングする。 グルーピングしたデータを別々のシートに分割する。シートの名前には日付を入れる。 最後に新しいエクセルファイルに保存する。

import openpyxl def split_excel(filepath): # Excelファイルを開く wb = openpyxl.load_workbook(filepath) # アクティブなシートを取得 ws = wb.active # 3列目のデータのみを取り出す dates = [cell.value for row in ws.iter_rows(min_row=2, max_col=3, max_row=ws.max_row) for cell in row] # 同じ日付のデータをまとめる grouped = {} for date in dates: if date not in grouped: grouped[date] = [] grouped[date].append(date) # 元のシートからデータを削除 ws.delete_rows(2, ws.max_row - 1) # 新しいシ

Questions about programming?Chat with your personal AI assistant