Setting Cell value doesn't use column format

Issue #963 wontfix
Alain Begey created an issue

Hi everyone, I don't know if it's a openpyxl issue, an Excel issue or something that will never work but here is my issue :

When I'm using a source Excel file with two columns : - in the first one I have a cell that I formatted directly (like color font = red) - in the second one I formatted the column and when I type somewhere the format is applied

With openpyl change the value of the cell with worksheet.cell function, The cell formatted directly is still formated, and the cell formatted through the colum is not...

You can reproduce it with docker-compose build and docker-compose up or by running with python 2.7 and openpyxl 2.4.8 & 2.5.0

Is it a bug ? An enhancement ? Thanks in advance

Comments (6)

  1. CharlieC

    Row and column formats are poorly specified. The documentation says they're notes for the application to apply to new cells created. In openpyxl we only preserve this as metadata which you can view and edit but which is otherwise is ignored. This dramatically simplifies the library's code because we don't have to look up such styles when creating cells and I have currently no intention to change this.

    When you want to format a lot of cells the best thing is to use a named style.

  2. Alain Begey reporter

    Ok, thanks for the answer. Is there a workaround like manually in this case getting the format metadata for the column and reapply it on each cell ?

  3. CharlieC

    Should be pretty easy, yes. The following probably won't work out of the box but should give you an idea of how trivial this is.

    cd = ws.column_dimensions['A']
    for c in ws['A']:
        c.font = cd.font
  4. Alain Begey reporter

    Seems good but sorry it's strange : When I do that I got TypeError: expected <class 'openpyxl.styles.fonts.Font'> even if when I look into cd.font it's a openpyxl.styles.fonts.Font object ...

  5. Log in to comment