KOTA ITO

Generation

generate functionThu, 15 Dec 2022

openpyxlを使って、Excelファイルの3列目に含まれる日付にしたがって、データを日付ごとのシートに分割する。

import openpyxl def separate(): wb = openpyxl.load_workbook('test.xlsx') ws = wb.worksheets[0] for row in range(2, ws.max_row + 1): date = ws.cell(row=row, column=3).value if date in wb.sheetnames: wb.copy_worksheet(wb[date]) wb.active = wb.sheetnames.index(date) wb.active.max_row += 1 else: wb.create_sheet(title=date) wb.active = wb.sheetnames.index(date) wb.active.append(ws[row]) wb.save('test_out.xlsx') if __name__ == '__main__': separate()

Questions about programming?Chat with your personal AI assistant