问题
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:
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
I've tried setting auto_size:
for dimension in fb.column_dimensions.values():
dimension.auto_size = True
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
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
Openpyxl: 2.2.2
回答1:
It doesn't seem to be an issue of column widths, but the columns themselves are hidden. Can you post your full code?
A workaround might be fb.column_dimensions['B'].hidden = False
Additionally, I would update to the newest version of openpyxl with pip install openpyxl
来源:https://stackoverflow.com/questions/32642026/column-widths-of-some-columns-in-openpyxl-become-zero-after-60-columns