Skip to content Skip to sidebar Skip to footer

How Can I List All 1st Row Values In An Excel Spreadsheet Using Openpyxl?

Using the OpenPyXL module with Python 3.5, I was able to figure out how many columns there are in a spreadsheet with: In [1]: sheet.max_column Out [1]: 4 Then I was able to list t

Solution 1:

If you need to iterate through all the rows or columns of a file, you can instead use the openpyxl.worksheet.Worksheet.rows() property, or the openpyxl.worksheet.Worksheet.columns() property.

See Manipulating a workbook in memory.

Solution 2:

Edit your sample to get what you want. But it's not recommended to do it this way!

for rowOfCellObjects in sheet['A1':sheet.cell(row=1, column=sheet.max_column).coordinate]:
      for cellObj in rowOfCellObjects:
          print(cellObj.coordinate, cellObj.value)

Use one of the following samples:

Sampel1: Iter all columns per row, break after the first row.

forrowsin ws.rows:
    forcellin rows:
      print('cell %s %s' % (cell.coordinate,cell.value))
    break

More programatical control:

Sample2: Iterates all columns only from the row given by min/max_row arguments, ends after one row.

Arguments min/max_row can point to any row, even also outside data.

forrowsin ws.iter_rows(min_row=1, max_row=1, min_col=1):
    forcellin rows:
      print('cell %s %s' % (cell.coordinate,cell.value))

* Tested with Python:3.4.2 - openpyxl:2.4.1 *

Post a Comment for "How Can I List All 1st Row Values In An Excel Spreadsheet Using Openpyxl?"