Some of the solutions above don't quite work very well (maybe because of latest version of 'openpyxl'). After trying out different things, I used this:
Printing all rows with all columns:
import openpyxl
sheet = openpyxl.load_workbook('myworkbook.xlsx')['Sheet1']
# Iterating through All rows with all columns...
for i in range(1, sheet.max_row+1):
row = [cell.value for cell in sheet[i]] # sheet[n] gives nth row (list of cells)
print(row) # list of cell values of this row
Printing all rows with specific columns (e.g. 'E' to 'L'):
# For example we need column 'E' to column 'L'
start_col = 4 # 'E' column index
end_col = 11 # 'L' column index
for i in range(1, sheet.max_row+1):
row = [cell.value for cell in sheet[i][start_col:end_col+1]]
print(row) # list of cell values of this row
Please keep these points in mind:
- sheet[N] gives the list of 'Cell' objects of Nth row. (N is a number starting from 1)
- To get the first column cell of a row, use sheet[N][0]. (Because sheet[N] is a 'tuple' which can be indexed starting from zero 0).