Column and row dimensions in OpenPyXL are always None

Tom picture Tom · Sep 30, 2015 · Viewed 22k times · Source

Why is openpyxl reading every row and column dimension as None? This is the case regardless of whether the table was created via openpyxl or within Microsoft Excel.

import openpyxl
wb = openpyxl.load_workbook(r'C:\data\MyTable.xlsx')
ws = wb.active
print ws.row_dimensions[1].height
print ws.column_dimensions['A'].width

prints None and None. These aren't hidden columns/rows. They clearly have dimensions when viewed in Excel.

I know that loading the workbook with iterators will prevent the dimension dictionaries from being created, but that results in key errors, and I'm not using iterators here.

Is there an alternative way to determine the width/height of a cell/row/column?

===============SOLUTION=================

Thanks to Charlie, I realized that the following is the best way to get a list of all row heights:

import openpyxl
wb = openpyxl.load_workbook(r'C:\Data\Test.xlsx')
ws = wb.active
rowHeights = [ws.row_dimensions[i+1].height for i in range(ws.max_row)]
rowHeights = [15 if rh is None else rh for rh in rowHeights]

Answer

Charlie Clark picture Charlie Clark · Sep 30, 2015

RowDimension and ColumnDimension objects exist only when the defaults are to be overwritten. So ws.row_dimensions[1].height will be always be None until it is assigned a value.

The default values are: {'defaultRowHeight': '15', 'baseColWidth': '10'}