question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent behavior of "set_column" formatting in constant memory mode

See original GitHub issue

We are using xlsxwriter (verison 3.0.3 on Python 3.8.14) to create and style excel files. When using constant memory mode, we found inconsistent behavior of how the set_column method applies cell formatting. Here is an example to demonstrate the issue:

import xlsxwriter

with open("test.xlsx", "wb") as fs:
    workbook = xlsxwriter.Workbook(fs, {"constant_memory": True})
    worksheet = workbook.add_worksheet("Test")

    worksheet.write_row(0, 0, data=[100000.123, 100000.123, 100000.123, 100000.123])
    worksheet.write_row(1, 0, data=[100000.123, 100000.123, 100000.123, 100000.123])
    worksheet.write_row(2, 0, data=[100000.123, 100000.123, 100000.123, 100000.123])

    worksheet.set_column(0, 3, cell_format=workbook.add_format({"num_format": "#,##0.00"}))

    workbook.close()

The resulting excel file looks like this. You can see that the number format was only applied to the last row:

Screenshot 2022-12-09 at 12 20 47

If I set {"constant_memory": False}, the file looks like expected:

Screenshot 2022-12-09 at 12 49 41

The same (correct) result can also be achieved by moving the styling above writing the data:

import xlsxwriter

with open("test.xlsx", "wb") as fs:
    workbook = xlsxwriter.Workbook(fs, {"constant_memory": True})
    worksheet = workbook.add_worksheet("Test")

    worksheet.set_column(0, 3, cell_format=workbook.add_format({"num_format": "#,##0.00"}))

    worksheet.write_row(0, 0, data=[100000.123, 100000.123, 100000.123, 100000.123])
    worksheet.write_row(1, 0, data=[100000.123, 100000.123, 100000.123, 100000.123])
    worksheet.write_row(2, 0, data=[100000.123, 100000.123, 100000.123, 100000.123])

    workbook.close()

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Birne94commented, Dec 9, 2022

Thank you @jmcnamara for the quick response.

Do you know why only the last column gets the correct style in this case?

I think this limitation should be worth mentioning in the documentation, especially since a lot of examples we found were following the pattern of writing data first and then applying the styling. Alternatively, maybe the library could also raise an exception instead, what do you think?

0reactions
jmcnamaracommented, Dec 9, 2022

Another interesting finding is that when looking at the generated excel files, we found the styles were correctly applied to the column definitions in the XML and were just missing in the cell definition:

That is for the reason explained above: the library writes the row to disk once a new row is initiated and if there isn’t a column or row format available at that time then the format isn’t applied.

This shouldn’t be an issue if you set the column formats first like in your first example.

The cell styling that works should look like this:

I am 100% aware of that. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Memory and Performance - XlsxWriter
By default XlsxWriter holds all cell data in memory. This is to allow future features when formatting is applied separately from the data....
Read more >
Lection C: Excel tables with inconsistent column formats
The focus lies on the error message 'Inconsistent column format'. ... column (excluding the heading) of the connected Excel table have a consistent...
Read more >
Database Engine events and errors - SQL Server
Consult this MSSQL error code list to find explanations for error messages for SQL Server database engine events.
Read more >
pandas/frame.py at main · pandas-dev/pandas - GitHub
Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, ...
Read more >
Package 'data.table' - R Project
The default value for cols is all the columns, to be consistent with the default behaviour of stats::na.omit. It does not add the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found