Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

python - Column widths (of some columns) in Openpyxl become zero after 60+ columns

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:

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...