OpenPyXL 中的列和行维度总是 None
Column and row dimensions in OpenPyXL are always None
为什么 openpyxl 将每一行和每一列维度读取为 None?无论 table 是通过 openpyxl 还是在 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
打印 None 和 None。这些没有隐藏 columns/rows。在 Excel.
中查看时,它们显然具有尺寸
我知道用迭代器加载工作簿会阻止创建维度字典,但这会导致键错误,我在这里不使用迭代器。
是否有其他方法来确定 cell/row/column 的 width/height?
===============解决方案=================
感谢 Charlie,我意识到以下是获取所有行高列表的最佳方法:
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]
RowDimension
和 ColumnDimension
对象仅在要覆盖默认值时才存在。所以 ws.row_dimensions[1].height
将永远是 None
直到它被赋值。
默认值为:{'defaultRowHeight': '15', 'baseColWidth': '10'}
openpyxl: 3.0.4
源代码:from openpyxl.worksheet.dimensions import SheetFormatProperties
显示 {'defaultRowHeight': 15, 'baseColWidth': 8}
# dimensions.py
class SheetFormatProperties(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
...
一个例子
from openpyxl import load_workbook
from openpyxl.worksheet.dimensions import SheetFormatProperties
from openpyxl.worksheet.worksheet import Worksheet
wb = load_workbook('xxx.xlsx')
for sheet in [wb[sheet_name] for sheet_name in wb.sheetnames]:
sheet: Worksheet
sheet_prop: SheetFormatProperties = sheet.sheet_format
default_width = sheet_prop.baseColWidth
default_height = sheet_prop.defaultRowHeight
为什么 openpyxl 将每一行和每一列维度读取为 None?无论 table 是通过 openpyxl 还是在 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
打印 None 和 None。这些没有隐藏 columns/rows。在 Excel.
中查看时,它们显然具有尺寸我知道用迭代器加载工作簿会阻止创建维度字典,但这会导致键错误,我在这里不使用迭代器。
是否有其他方法来确定 cell/row/column 的 width/height?
===============解决方案=================
感谢 Charlie,我意识到以下是获取所有行高列表的最佳方法:
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]
RowDimension
和 ColumnDimension
对象仅在要覆盖默认值时才存在。所以 ws.row_dimensions[1].height
将永远是 None
直到它被赋值。
默认值为:{'defaultRowHeight': '15', 'baseColWidth': '10'}
openpyxl: 3.0.4
源代码:from openpyxl.worksheet.dimensions import SheetFormatProperties
显示 {'defaultRowHeight': 15, 'baseColWidth': 8}
# dimensions.py
class SheetFormatProperties(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
...
一个例子
from openpyxl import load_workbook
from openpyxl.worksheet.dimensions import SheetFormatProperties
from openpyxl.worksheet.worksheet import Worksheet
wb = load_workbook('xxx.xlsx')
for sheet in [wb[sheet_name] for sheet_name in wb.sheetnames]:
sheet: Worksheet
sheet_prop: SheetFormatProperties = sheet.sheet_format
default_width = sheet_prop.baseColWidth
default_height = sheet_prop.defaultRowHeight