OpenpyXL를 이용하여 Excel 파일 읽기
- 파이썬 Excel 2010 Library (xlsx/xlsm/xltx/xltm 파일)
Install
$ pip install openpyxl
Simple Sample Code
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = 42
ws.append([1, 2, 3])
import datetime
ws['A2'] = datetime.datetime.now()
wb.save("sample.xlsx")
How to Read Excel
Open File
import openpyxl
excel_document = openpyxl.load_workbook('sample.xlsx')
print type(excel_document)
Sheet 정보 가지고 오기
excel_document.get_sheet_names()
Cell에 접근하기
sheet = excel_document.get_sheet_by_name('Sheet1')
print sheet['A2'].value
print sheet.cell(row = 5, column = 2).value
print type(sheet['A2'])
print sheet.cell(row = 5, column = 2)
Cell의 범위에 접근하기
multiple_cells = sheet['A1':'C2']
for row in multiple_cells:
for cell in row:
print cell.value
모든 행과 열 접근하기
all_rows = sheet.rows
for row in all_rows:
print row.value
all_columns = sheet.columns
for column in all_columns
print column.value