/
openpyxl_example.py
146 lines (104 loc) · 3.72 KB
/
openpyxl_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import openpyxl
import pprint
wb = openpyxl.load_workbook('data/src/sample.xlsx')
print(type(wb))
# <class 'openpyxl.workbook.workbook.Workbook'>
print(wb.sheetnames)
# ['sheet1', 'sheet2']
sheet = wb['sheet1']
print(type(sheet))
# <class 'openpyxl.worksheet.worksheet.Worksheet'>
cell = sheet['A2']
print(type(cell))
# <class 'openpyxl.cell.cell.Cell'>
print(cell.value)
# one
cell = sheet.cell(row=2, column=1)
print(type(cell))
# <class 'openpyxl.cell.cell.Cell'>
print(cell.value)
# one
pprint.pprint(sheet['A2:C4'])
# ((<Cell 'sheet1'.A2>, <Cell 'sheet1'.B2>, <Cell 'sheet1'.C2>),
# (<Cell 'sheet1'.A3>, <Cell 'sheet1'.B3>, <Cell 'sheet1'.C3>),
# (<Cell 'sheet1'.A4>, <Cell 'sheet1'.B4>, <Cell 'sheet1'.C4>))
g = sheet.iter_rows(min_row=2, max_row=4, min_col=1, max_col=3)
print(type(g))
# <class 'generator'>
pprint.pprint(list(g))
# [(<Cell 'sheet1'.A2>, <Cell 'sheet1'.B2>, <Cell 'sheet1'.C2>),
# (<Cell 'sheet1'.A3>, <Cell 'sheet1'.B3>, <Cell 'sheet1'.C3>),
# (<Cell 'sheet1'.A4>, <Cell 'sheet1'.B4>, <Cell 'sheet1'.C4>)]
def get_value_list(t_2d):
return([[cell.value for cell in row] for row in t_2d])
l_2d = get_value_list(sheet['A2:C4'])
pprint.pprint(l_2d, width=40)
# [['one', 11.0, 12.0],
# ['two', 21.0, 22.0],
# ['three', 31.0, 32.0]]
def get_list_2d(sheet, start_row, end_row, start_col, end_col):
return get_value_list(sheet.iter_rows(min_row=start_row,
max_row=end_row,
min_col=start_col,
max_col=end_col))
l_2d = get_list_2d(sheet, 2, 4, 1, 3)
pprint.pprint(l_2d, width=40)
# [['one', 11.0, 12.0],
# ['two', 21.0, 22.0],
# ['three', 31.0, 32.0]]
g_all = sheet.values
print(type(g_all))
# <class 'generator'>
pprint.pprint(list(g_all), width=40)
# [(None, 'A', 'B', 'C'),
# ('one', 11.0, 12.0, 13.0),
# ('two', 21.0, 22.0, 23.0),
# ('three', 31.0, 32.0, 33.0)]
sheet['C1'] = 'XXX'
sheet['E1'] = 'new'
pprint.pprint(list(sheet.values), width=40)
# [(None, 'A', 'XXX', 'C', 'new'),
# ('one', 11.0, 12.0, 13.0, None),
# ('two', 21.0, 22.0, 23.0, None),
# ('three', 31.0, 32.0, 33.0, None)]
sheet.cell(row=2, column=5, value=14)
pprint.pprint(list(sheet.values), width=40)
# [(None, 'A', 'XXX', 'C', 'new'),
# ('one', 11.0, 12.0, 13.0, 14),
# ('two', 21.0, 22.0, 23.0, None),
# ('three', 31.0, 32.0, 33.0, None)]
def write_list_2d(sheet, l_2d, start_row, start_col):
for y, row in enumerate(l_2d):
for x, cell in enumerate(row):
sheet.cell(row=start_row + y,
column=start_col + x,
value=l_2d[y][x])
l_2d = [['four', 41, 42, 43], ['five', 51, 52, 53]]
write_list_2d(sheet, l_2d, 5, 1)
pprint.pprint(list(sheet.values), width=40)
# [(None, 'A', 'XXX', 'C', 'new'),
# ('one', 11.0, 12.0, 13.0, 14),
# ('two', 21.0, 22.0, 23.0, None),
# ('three', 31.0, 32.0, 33.0, None),
# ('four', 41, 42, 43, None),
# ('five', 51, 52, 53, None)]
sheet_new = wb.create_sheet('sheet_new')
print(wb.worksheets)
# [<Worksheet "sheet1">, <Worksheet "sheet2">, <Worksheet "sheet_new">]
sheet_new['A1'] = 'new sheet!'
print(list(sheet_new.values))
# [('new sheet!',)]
sheet_copy = wb.copy_worksheet(wb['sheet1'])
print(wb.worksheets)
# [<Worksheet "sheet1">, <Worksheet "sheet2">, <Worksheet "sheet_new">, <Worksheet "sheet1 Copy">]
pprint.pprint(list(sheet_copy.values))
# [(None, 'A', 'XXX', 'C', 'new'),
# ('one', 11.0, 12.0, 13.0, 14),
# ('two', 21.0, 22.0, 23.0, None),
# ('three', 31.0, 32.0, 33.0, None),
# ('four', 41, 42, 43, None),
# ('five', 51, 52, 53, None)]
wb.remove_sheet(wb['sheet1 Copy'])
print(wb.worksheets)
# [<Worksheet "sheet1">, <Worksheet "sheet2">, <Worksheet "sheet_new">]
wb.save('data/dst/openpyxl_sample.xlsx')