openpyxl
은 파이썬에서 엑셀을 다루는 것을 쉽게 해주는 도구이다.
Excel 에 데이터 쓰기
from openpyxl import Workbook
write_wb = Workbook()
write_ws = write_wb.create_sheet('생성시트')
write_ws = write_wb.active
write_ws['A1'] = '숫자'
write_ws.append([1,2,3])
write_ws.cell(5, 5, '5행5열')
write_wb.save("C:/Users/Administrator/Desktop/기준/프로그래밍/과제대행/주식데이터크롤링/숫자.xlsx")
Excel 데이터 읽기
from openpyxl import load_workbook
load_wb = load_workbook("C:/Users/Administrator/Desktop/기준/프로그래밍/과제대행/주식데이터크롤링/output.xlsx", data_only=True)
load_ws = load_wb['Sheet1']
print(load_ws['B2'].value)
print(load_ws.cell(3, 2).value)
get_cells = load_ws['B3' : 'B6']
for row in get_cells:
for cell in row:
print(cell.value)
for row in load_ws.rows:
print(row)
for column in load_ws.columns:
print(column)
all_values = []
for row in load_ws.rows:
row_value = []
for cell in row:
row_value.append(cell.value)
all_values.append(row_value)
print(all_values)
load_ws.cell(3, 3, 51470)
load_ws.cell(4, 3, 21470)
load_ws.cell(5, 3, 1470)
load_ws.cell(6, 3, 6470)
load_wb.save("C:/Users/Administrator/Desktop/기준/프로그래밍/과제대행/주식데이터크롤링/output.xlsx")
참조: 3.0 파이썬 엑셀 다루기 - openpyxl 사용법