본문 바로가기

카테고리 없음

파이썬, openpyxl(2) - 엑셀 불러오기

반응형
from openpyxl import workbook
from random import *

wb = Workbook()
ws = wb.active

ws.append(["번호", "영어", "수학"])
for i in range(1, 11):
    ws.append([i, randint(1, 100), randint(1, 100)])
    
#영어 col만 가져오
col_B = ws["B"] 
# print(col_B)
# for cell in col_B:
#     print(cell.value)

#영어, 수학 col 가져오기
col_range = ws["B:C"]
for cols in col_range:
    for cell in cols:
        print(cell.value)
    print()

    
#입력되어 있는 최대 row, col까지 가져오기
for x in range(1, ws.max_row):
    for y in range(1, ws.max_column):
        print(ws.cell(row = x, column = y).value, end=' ')
    print()
    
#튜블 형태로 row[1] 데이터 가져오기
for row in tuple(ws.rows):
    print(row[1].value)

#범위 내에서 가져오기
for row in ws.iter_rows(min_row=1, max_row=5, min_column=2, max_column = 3):
    #()안에 범위가 없으면 전체데이터 가져오기
    #inter_rows는 좌에서 우로 데이터 가져옴, inter_cols는 상에서 하로 데이터 가져옴
반응형