Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column and row dimensions in OpenPyXL are always None

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]
like image 760
Tom Avatar asked Sep 30 '15 00:09

Tom


1 Answers

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'}

like image 64
Charlie Clark Avatar answered Oct 20 '22 12:10

Charlie Clark