Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a table in XlsxWriter using filter's 'Sort A to Z'?

Recently, I've discovered XlsxWriter for Python (and I do enjoy it!) and I started working on some spreadsheet. I need to programmatically create a spreadsheet identical to one created manually by some other person. I managed to create everything I needed except for sorting table in autofilter. Basically, I would like to create something like this:Manually created autofiler and I couldn't figure out how. I checked with the documentation (1, 2) and didn't find any information about sorting. What I do now is:

worksheet.autofilter(1, 0, known_lenght, known_width)

which results in autofilter arrow appearing in lower right corner of header cells but worksheet.autofilter() seems to lack sorting capability. Am I missing something or is there no option to create autofilter and set sorting in it in XlsxWriter? Thanks!

like image 317
Artur Avatar asked Dec 23 '22 17:12

Artur


1 Answers

Am I missing something or is there no option to create autofilter and set sorting in it in XlsxWriter?

Sorting of data in Excel is done at runtime and isn't a function of the file format. As such it isn't supported by XlsxWriter.

Instead you should sort the data in Python and then write it to the autofilter region.

However, that won't set the autofilter arrow to "sorted". That isn't currently supported in XlsxWriter.

like image 159
jmcnamara Avatar answered Jan 05 '23 19:01

jmcnamara