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

11,366

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

Share:
11,366
Pyderman
Author by

Pyderman

Some day, I'll be one of the ones answering questions. Until then, I endeavour to submit articulate, well thought-out questions. If you find issue with any of my questions, please add a comment stating why (rather than simply downvoting). That way, I and other submitters can improve.

Updated on June 04, 2022

Comments

  • Pyderman
    Pyderman almost 2 years

    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