Column and row dimensions in OpenPyXL are always None

别来无恙 提交于 2019-12-12 09:36:31

问题


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]

回答1:


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



来源:https://stackoverflow.com/questions/32855656/column-and-row-dimensions-in-openpyxl-are-always-none

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!