I tracked down some unexpected behavior that can 'hide columns' in 2.4.5; I think it would be worth fixing.
When Excel has consecutive columns with the same formatting it consolidates them to one customWidth xml item, such as this for columns M and N (columns 13 and 14):
<col customWidth="1" style="4" width="8.42578125" max="14" min="13"/>
If I add a cell in column N in openpyxl, then it will have default formatting, rather than the formatting of the column, so I copy formatting from it's host column like so:
def copy_column_format(self, cell): # Copy column formatting cell.number_format = cell.parent.column_dimensions[cell.column].number_format cell.font = cell.parent.column_dimensions[cell.column].font.copy() cell.alignment = cell.parent.column_dimensions[cell.column].alignment.copy()
Little did I realize when I wrote the code that there is no
column_dimension['N'], and that openpyxl would then automatically and quietly create the missing column_dimension with default settings. Thus the new column_dimension has default formatting and a width of 0.
When the sheet is subsequently saved and opened in Excel the column is now hidden, and the data I wrote and column have the wrong formatting.
My workaround will be to check if the column_dimension exists, and if not found keep iterating back to find the relevant preceding column_dimension.
One proposal for openpyxl is that when new cells are created there is an option to apply formatting for that column (the same way Excel does). Perhaps my workaround could also be implemented to avoid the confusing behavior.
Note I also saw this issue on stackoverflow, but I'm not sure if it's the same thing: http://stackoverflow.com/questions/29661324/column-set-to-hidden-when-editing-existing-xlsx-file