Skip to content Skip to sidebar Skip to footer

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

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

Solution 2:

openpyxl: 3.0.4

source code: from openpyxl.worksheet.dimensions import SheetFormatProperties

it shows the {'defaultRowHeight': 15, 'baseColWidth': 8}

# dimensions.pyclassSheetFormatProperties(Serialisable):

    ...

    def__init__(self,
                 baseColWidth=8,  # <-----------------
                 defaultColWidth=None,
                 defaultRowHeight=15,  # <------------
                 customHeight=None,
                 zeroHeight=None,
                 thickTop=None,
                 thickBottom=None,
                 outlineLevelRow=None,
                 outlineLevelCol=None,
                ):
        self.baseColWidth = baseColWidth
        self.defaultColWidth = defaultColWidth
        self.defaultRowHeight = defaultRowHeight
        self.customHeight = customHeight
        self.zeroHeight = zeroHeight
        ...

an example

from openpyxl import load_workbook
from openpyxl.worksheet.dimensions import SheetFormatProperties
from openpyxl.worksheet.worksheet importWorksheetwb= load_workbook('xxx.xlsx')
for sheet in [wb[sheet_name] for sheet_name in wb.sheetnames]:
    sheet: Worksheet
    sheet_prop: SheetFormatProperties = sheet.sheet_formatdefault_width= sheet_prop.baseColWidthdefault_height= sheet_prop.defaultRowHeight

Post a Comment for "Column And Row Dimensions In Openpyxl Are Always None"