반응형
from openpyxl.utils.cell import coordinate_from_string
from openpyxl import Workbook
from random import *

wb = Workbook()
ws = wb.active

# 1줄씩 데이터 넣기
ws.append(["번호", "영어", "수학"])

for i in range(1, 11):  # 10개 데이터 넣기
    ws.append([i, randint(0, 100), randint(0, 100)])

col_B = ws["B"]  # 영어 column만 가져오기
print(col_B)

for cell in col_B:
    print(cell.value)

col_range = ws["B:C"]

for cols in col_range:  # 영어, 수학 column 모두 가져오기
    for cell in cols:
        print(cell.value)


row_title = ws[1]  # 1번째 row만 가져오기
for cell in row_title:
    print(cell.value)

row_range = ws[1:6]  # 1번째 줄에서 6번째 줄까지 가져오기 (슬라이싱은 2~5지만 엑셀에서는 2~6)
for rows in row_range:
    for cell in rows:
        print(cell.value, end=" ")
    print()


row_range = ws[2:ws.max_row]  # 2번째 줄에서 마지막 줄까지 가져오기 (슬라이싱은 2~5지만 엑셀에서는 2~6)
for rows in row_range:
    for cell in rows:
        print(cell.coordinate, end=" ")  # 현재 작업중인 좌표 위치를 알아낼 때
        xy = coordinate_from_string(cell.coordinate)  # 좌표의 x,y를 튜플로 표기
        print(xy, end=" ")
        # print(cell.value, end=" ")
    print()


# 전체 rows
print(" == 전체 rows == ")
print(tuple(ws.rows))

# 각 행의 1번 idx 값들을 출력
for row in tuple(ws.rows):
    print(row[1].value)

# 전체 columns
print(" == 전체 columns == ")
print(tuple(ws.columns))

# 각 열의 0번 idx 값들을 출력
for column in tuple(ws.columns):
    print(column[0].value)

# 각 열의 1번 데이터 출력
for column in ws.iter_cols():
    print(column[1].value)

# 1번부터 5번행 사이의 1번 데이터 출력
for row in ws.iter_rows(min_row=1, max_row=5):
    print(row[1].value)

# 1번부터 5번째 행사이, 2번 열부터 3번 열사이의 1번 데이터 출력
for row in ws.iter_rows(min_row=1, max_row=5, min_col=2, max_col=3):
    print(row[1].value)

# 1번부터 5번째 행사이, 2번 열부터 3번 열사이의 1번 데이터 출력
for col in ws.iter_cols(min_row=1, max_row=5, min_col=2, max_col=3):
    print(col[1].value)

wb.save("crocus_cell_range.xlsx")
wb.close()

 

 

# 1줄씩 데이터 넣기
ws.append(["번호", "영어", "수학"])

for i in range(1, 11):  # 10개 데이터 넣기
    ws.append([i, randint(0, 100), randint(0, 100)])

ws.append를 이용하면 현재 행에 데이터를 한줄씩 추가할 수 있다.

 

col_B = ws["B"]  # 영어 column만 가져오기
print(col_B)

for cell in col_B:
    print(cell.value)

col_range = ws["B:C"]

for cols in col_range:  # 영어, 수학 column 모두 가져오기
    for cell in cols:
        print(cell.value)

ws["B"]를 하면 B열의 데이터들을 가져올 수 있게되고 아래처럼 for문을 이용하면 B열 데이터를 모두 출력 할 수 있다.

 

그리고 col_range를 통해서도 열의 범위를 정해서 그사이의 모든 데이터도 가져 올 수 있다.

 

 

row_title = ws[1]  # 1번째 row만 가져오기
for cell in row_title:
    print(cell.value)

row_range = ws[1:6]  # 1번째 줄에서 6번째 줄까지 가져오기 (슬라이싱은 2~5지만 엑셀에서는 2~6)
for rows in row_range:
    for cell in rows:
        print(cell.value, end=" ")
    print()


row_range = ws[2:ws.max_row]  # 2번째 줄에서 마지막 줄까지 가져오기 (슬라이싱은 2~5지만 엑셀에서는 2~6)
for rows in row_range:
    for cell in rows:
        print(cell.coordinate, end=" ")  # 현재 작업중인 좌표 위치를 알아낼 때
        xy = coordinate_from_string(cell.coordinate)  # 좌표의 x,y를 튜플로 표기
        print(xy, end=" ")
        # print(cell.value, end=" ")
    print()

 

row도 마찬가지로 데이터를 위와 같이 가져올 수 있다.

 

cell.coordinate를 이용하면 다음과 같은 출력물을 얻을 수 있다.

A2 ('A', 2) B2 ('B', 2) C2 ('C', 2)
A3 ('A', 3) B3 ('B', 3) C3 ('C', 3)
A4 ('A', 4) B4 ('B', 4) C4 ('C', 4)
A5 ('A', 5) B5 ('B', 5) C5 ('C', 5)
A6 ('A', 6) B6 ('B', 6) C6 ('C', 6)
A7 ('A', 7) B7 ('B', 7) C7 ('C', 7)
A8 ('A', 8) B8 ('B', 8) C8 ('C', 8)
A9 ('A', 9) B9 ('B', 9) C9 ('C', 9)
A10 ('A', 10) B10 ('B', 10) C10 ('C', 10)
A11 ('A', 11) B11 ('B', 11) C11 ('C', 11)

 

 

 

(<Cell 'Sheet'.B1>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.B4>, <Cell 'Sheet'.B5>, <Cell 'Sheet'.B6>, <Cell 'Sheet'.B7>, <Cell 'Sheet'.B8>, <Cell 'Sheet'.B9>, <Cell 'Sheet'.B10>, <Cell 'Sheet'.B11>)
영어
42
3
80
13
50
74
79
70
47
76
영어
42
3
80
13
50
74
79
70
47
76
수학
68
61
61
99
100
59
60
54
98
33
번호
영어
수학
번호 영어 수학
1 42 68
2 3 61
3 80 61
4 13 99
5 50 100
A2 ('A', 2) B2 ('B', 2) C2 ('C', 2)
A3 ('A', 3) B3 ('B', 3) C3 ('C', 3)
A4 ('A', 4) B4 ('B', 4) C4 ('C', 4)
A5 ('A', 5) B5 ('B', 5) C5 ('C', 5)
A6 ('A', 6) B6 ('B', 6) C6 ('C', 6)
A7 ('A', 7) B7 ('B', 7) C7 ('C', 7)
A8 ('A', 8) B8 ('B', 8) C8 ('C', 8)
A9 ('A', 9) B9 ('B', 9) C9 ('C', 9)
A10 ('A', 10) B10 ('B', 10) C10 ('C', 10)
A11 ('A', 11) B11 ('B', 11) C11 ('C', 11)
 == 전체 rows ==
((<Cell 'Sheet'.A1>, <Cell 'Sheet'.B1>, <Cell 'Sheet'.C1>), (<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>), (<Cell 'Sheet'.A3>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.C3>), (<Cell 'Sheet'.A4>, <Cell 'Sheet'.B4>, <Cell 'Sheet'.C4>), (<Cell 'Sheet'.A5>, <Cell 'Sheet'.B5>, <Cell 'Sheet'.C5>), (<Cell 'Sheet'.A6>, <Cell 'Sheet'.B6>, <Cell 'Sheet'.C6>), (<Cell 'Sheet'.A7>, <Cell 'Sheet'.B7>, <Cell 'Sheet'.C7>), (<Cell 'Sheet'.A8>, <Cell 'Sheet'.B8>, <Cell 'Sheet'.C8>), (<Cell 'Sheet'.A9>, <Cell 'Sheet'.B9>, <Cell 'Sheet'.C9>), (<Cell 'Sheet'.A10>, <Cell 'Sheet'.B10>, <Cell 'Sheet'.C10>), (<Cell 'Sheet'.A11>, <Cell 'Sheet'.B11>, <Cell 'Sheet'.C11>))  
영어
42
3
80
13
50
74
79
70
47
76
 == 전체 columns ==
((<Cell 'Sheet'.A1>, <Cell 'Sheet'.A2>, <Cell 'Sheet'.A3>, <Cell 'Sheet'.A4>, <Cell 'Sheet'.A5>, <Cell 'Sheet'.A6>, <Cell 'Sheet'.A7>, <Cell 'Sheet'.A8>, <Cell 'Sheet'.A9>, <Cell 'Sheet'.A10>, <Cell 'Sheet'.A11>), (<Cell 'Sheet'.B1>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.B4>, <Cell 'Sheet'.B5>, <Cell 'Sheet'.B6>, <Cell 'Sheet'.B7>, <Cell 'Sheet'.B8>, <Cell 'Sheet'.B9>, <Cell 'Sheet'.B10>, <Cell 'Sheet'.B11>), (<Cell 'Sheet'.C1>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.C3>, <Cell 'Sheet'.C4>, <Cell 'Sheet'.C5>, <Cell 'Sheet'.C6>, <Cell 'Sheet'.C7>, <Cell 'Sheet'.C8>, <Cell 'Sheet'.C9>, <Cell 'Sheet'.C10>, <Cell 'Sheet'.C11>))
번호
영어
수학
1
42
68
영어
42
3
80
13
수학
68
61
61
99
42
68
반응형