본문 바로가기

Python_Intermediate/Openpyxl

Python Openpyxl - Sheet Delete(excel file)

반응형

1. excle 파일에 sheet를 삭제하는 방법


2. Openpyxl 모듈 임포트

from openpyxl import Workbook


3. sheet del 기본 사용법

wb.remove(wb['삭제할 시트 제목'])


4. 코드 정리

sheet 안에 내용을 전체 삭제하는 명령어다.


5. 예제 코드

from openpyxl import Workbook

wb = Workbook()
dest_filename = 'sample.xlsx'

ws1 = wb.active
ws1.title = 'sample excel'
ws1['A1'] = 'Python excel'

ws2 = wb.create_sheet()
ws2.title = 'sample excel2'
ws2['A2'] = 'Python excel'

ws3 = wb.create_sheet()
ws3.title = 'sample excel3'
ws3['A3'] = 'Python excel'

wb.remove(wb['sample excel3'])

wb.save(filename = dest_filename)


6. 예제 코드 결과물

sample.xlsx

6-1. 삭제 전


6-2. 삭제 후


반응형