KOTA ITO

Generation

generate functionThu, 15 Dec 2022

openpyxlを使って、Excelファイルの3列目に含まれる日付ごとに、データを別々のシートに分割する。シートの名前には日付を入れる。

from openpyxl import load_workbook wb = load_workbook(filename = 'sample.xlsx') ws = wb.active wbnew = Workbook() for row in ws.iter_rows(min_row=1, min_col=3, max_col=3): for cell in row: # ここでExcelのDate型をdatetime型に変換 date = datetime.datetime.strptime(cell.value, '%Y-%m-%d %H:%M:%S') # 日付ごとにシートを作成 wsnew = wbnew.create_sheet(index=0, title=date.strftime('%Y%m%d')) wsnew.append(ws['A1:C1']) for row in ws.iter_rows(min_row=2, max_row=ws.max_row, min_col=1, max_col

Questions about programming?Chat with your personal AI assistant