OpenPyXL中的列和行尺寸始终为无 [英] Column and row dimensions in OpenPyXL are always None

查看:435
本文介绍了OpenPyXL中的列和行尺寸始终为无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么openpyxl将每一行和每一列的维读取为None?无论表是通过openpyxl还是在Microsoft Excel中创建的,都是这种情况.

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

打印无和无.这些不是隐藏的列/行.在Excel中查看时,它们显然具有尺寸.

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]

推荐答案

RowDimensionColumnDimension对象仅在覆盖默认值时存在.因此,在为ws.row_dimensions[1].height分配值之前,它将始终为None.

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.

默认值为:{'defaultRowHeight': '15', 'baseColWidth': '10'}

这篇关于OpenPyXL中的列和行尺寸始终为无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆