import openpyxl
wb = openpyxl.load_workbook('sample.xlsx')
>>> Unnamed: 0 A B C
>>> 0 one 11 12 13
>>> 1 two 21 22 23
>>> 2 three 31 32 33
wb.sheetnames
>>> ['sheet1', 'sheet2']
ws1 = wb['sheet1']
ws1['A2'].value # なぜ?
>>> 'one'
# 2次元のタプルを取得
ws1['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>))
# ws['A1:C4']のタプル形式を渡すと2次元配列が返ってくる
def get_value_list(t_2d):
return([[cell.value for cell in row] for row in t_2d])
l_2d = get_value_list(ws1['A2:C4'])
l_2d
>>> [['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(ws1, 2, 4, 1, 3)
l_2d
>>> [['one', 11.0, 12.0], ['two', 21.0, 22.0], ['three', 31.0, 32.0]]
https://www.self-study-blog.com/dokugaku/python-excel-openpyxl-cell-line-border-side-none/