Search code examples
pythonopenpyxlimport-from-excel

Openpyxl reads and returns a None every second cell


I'm trying to read a xlsx using the load_workbook method and it works fairly well. However, if there is an Empty cell in a column and the workbook is loaded with "use_iterators or read_only" option set, the rows() method adds a 'None' value to the output after every original cell value in that column. If there are no empty cells in the column within the data range, then there is no problem.

Please find below the piece of code and the data of the excel file I'm reading from:

Excel file: book1.xlsx

Data range: (Column A, row 1-7)

empty cell

1

empty cell

2

3

4

5

Code:

  Book=openpyxl.load_workbook(filename='book1.xlsx', read_only=True, data_only=True)
  ws=Book.get_sheet_by_name('sheet1')
  for row in ws.rows:
      for col in row:
           print col.value

Actual Output (when read_only option is true):

None

1

None

None

2

None

3

None

4

None

5

Expected Output:

None

1

None

2

3

4

5

I still couldn't resolve of this as I'm still facing the same issue. Please excuse me for any naive errors. I'm new to python and openpyxl and I'm here just looking for an answer to the problem. Any help would be highly appreciated.


Solution

  • As per the confirmation from "Charlie Clark" (in openpyxl google groups), this is a bug in the current version of openpyxl. This is applicable to any worksheet with empty rows due a counter not being updated. This is Fixed in the 2.2 branch and will be in 2.2.1 when released.