60列以上之后,Openpyxl中(某些列的)列宽变为零 [英] Column widths (of some columns) in Openpyxl become zero after 60+ columns
问题描述
给出一个包含5个工作表的wb
,我每天都在使用openpyxl
为其添加一列,并且运行良好.现在,尽管只有60多个列,但前N个列的宽度(似乎是 B 到 BH )的宽度已变为0.这将导致列在Excel中打开时基本上消失了:
Given a wb
with 5 worksheets, I am adding a column to each every day using openpyxl
, and this has been working well. Now though, with just over 60 columns, the width of the first N number of columns (seems to be B through to BH) has become 0. This results in the columns essentially disappearing when opened in Excel:
获取前几列的宽度可以确认这一点(其中fb
是<Worksheet>
对象):
Fetching the width of the first few columns confirms this (where fb
is a <Worksheet>
object):
In [71]: fb.column_dimensions["A"].width
Out[71]: 46.125
In [72]: fb.column_dimensions["B"].width
Out[72]: 0.0
In [73]: fb.column_dimensions["BI"].width
Out[73]: 11.75
In [73]: fb.column_dimensions["BJ"].width
Out[73]: 10.25
我尝试设置auto_size:
I've tried setting auto_size:
for dimension in fb.column_dimensions.values():
dimension.auto_size = True
,然后保存工作簿,但这没有效果(列在Excel中仍然不可见).所以我尝试手动将几列的大小设置为合理的大小,例如12:
and then saving the workbook, but this had no effect (columns are still invisible in Excel). So I tried manually setting the size of a couple of columns to a reasonable size e.g. 12:
fb.column_dimensions["B"].width = 12
再次保存,但仍然没有更改(列在Excel中仍然不可见),尽管这样的事实,当我在openpyxl中重新加载电子表格并检查列宽时,它们被设置为新的(非零) )大小.
and again saving, but still no change (columns are still invisible in Excel), despite the fact that when I reload the spreadsheet in openpyxl and check the columns width, they are set to the new (non-zero) size.
这两种方法中的一种还是两种都行得通,而我又缺少一个额外的步骤?
Should either or both of these approaches even have worked, and there is an additional step I'm missing?
Python:2.7.10
Python: 2.7.10
Openpyxl:2.2.2
Openpyxl: 2.2.2
推荐答案
这似乎不是列宽的问题,但是列本身是隐藏的.您可以发布完整的代码吗?
It doesn't seem to be an issue of column widths, but the columns themselves are hidden. Can you post your full code?
一种解决方法可能是fb.column_dimensions['B'].hidden = False
此外,我将使用pip install openpyxl
这篇关于60列以上之后,Openpyxl中(某些列的)列宽变为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!