youtube 나도코딩 강좌에서 연습용으로 코딩한 것입니다. (출처:www.youtube.com/watch?v=exgO1LFl9x8&t=3033s)

강좌 코드

sample1.xlsx
0.00MB

랑은 조금 다릅니다.

=================================

from openpyxl import Workbook

from random import *

wb = Workbook()

ws = wb.active

 

ws.append(["번호","영어","수학"])

for i in range(1,11):

    ws.append([i,randint(0,100),randint(0,100)])

 

col_B = ws["B"]

 

print(col_B)

 

for cell in col_B:

    print(cell.value)

 

col_range = ws["B:C"]

for cols in col_range:

    for cell in cols:

        print(cell.value)

 

row_title = ws[1]

for cell in row_title:

    print(cell.value)

 

row_range = ws[2:6]

for rows in row_range:

    for cell in rows:

        print(cell.value, end="  ")

    print()

 

row_range = ws[2:ws.max_row]

for rows in row_range:

    for cell in rows:

        print(cell.value, end=" ")

    print()

 

from openpyxl.utils.cell import coordinate_from_string

for rows in row_range:

    for cell in rows:

        print(cell.coordinate, end=" ")

        xy = coordinate_from_string(cell.coordinate)

        print(xy, end=" ")

        print(xy[0], end=" ")

        print(xy[1], end=" ")

        print()

    print()

 

wb.save("sample1.xlsx")

#wb = load_workbook("sample.xlsx")

 

====================================

실행결과

====================================

번호
영어
수학
1  34  46
2  95  19
3  40  33
4  35  48
5  85  66
1 34 46
2 95 19
3 40 33
4 35 48
5 85 66
6 41 4
7 71 54
8 74 29
9 62 6
10 58 95
A2 ('A', 2) A 2 
B2 ('B', 2) B 2
C2 ('C', 2) C 2

A3 ('A', 3) A 3
B3 ('B', 3) B 3
C3 ('C', 3) C 3

A4 ('A', 4) A 4
B4 ('B', 4) B 4
C4 ('C', 4) C 4

A5 ('A', 5) A 5
B5 ('B', 5) B 5
C5 ('C', 5) C 5 

A6 ('A', 6) A 6
B6 ('B', 6) B 6
C6 ('C', 6) C 6

A7 ('A', 7) A 7
B7 ('B', 7) B 7
C7 ('C', 7) C 7

A8 ('A', 8) A 8
B8 ('B', 8) B 8
C8 ('C', 8) C 8

A9 ('A', 9) A 9
B9 ('B', 9) B 9
C9 ('C', 9) C 9

A10 ('A', 10) A 10
B10 ('B', 10) B 10
C10 ('C', 10) C 10

A11 ('A', 11) A 11
B11 ('B', 11) B 11
C11 ('C', 11) C 11

PS C:\Users\android\coding\rpa> 

'Python' 카테고리의 다른 글

pyautogui 키보드 속성  (0) 2020.12.21

+ Recent posts