[python] 파이썬 xlsx 쓰기, openpyxl
1.쓰기.
from openpyxl import Workbook
wb = Workbook()
sheet1 = wb.active # 엑셀은 최소한 1개이상의 sheet는 존재. 기본적으로 존재하는 sheet을 가져올때 이렇게.
sheet1.title = 'Simple1'
abc = [1,2,'abc',4,55,3,45,5,2,32,43,3,2,2,3,5,5]
for rowindex in range(1,10):
sheet1.cell(row=rowindex, column=1).value = rowindex
sheet1.cell(row=rowindex, column=2).value = abc[(rowindex-1)]
# 이렇게 row와 ,col을 지정해가면서 값을 쓴다.
wb.save(filename='test.xlsx')
# 이렇게 파일 저장.
2. 기존의 파일에 새로운 시트 추가.
from openpyxl import load_workbook
wb = load_workbook('test.xlsx') #기존 파일 불러옴.
sheet2 = wb.create_sheet('sht2') #새로운 시트 만듦.
abc = [1,2,'abc',4,55,3,45,5,2,32,43,3,2,2,3,5,5]
for rowindex in range(1,10):
sheet2.cell(row=rowindex, column=1).value = rowindex
sheet2.cell(row=rowindex, column=2).value = abc[(rowindex-1)]
wb.save('sample2.xlsx') #파일로 다시 저장.