Inconsistent behavior of "set_column" formatting in constant memory mode
See original GitHub issueWe 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:
If I set {"constant_memory": False}
, the file looks like expected:
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:
- Created 9 months ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?
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.
I am 100% aware of that. 😃