jmcnamara / XlsxWriter

A Python module for creating Excel XLSX files.
https://xlsxwriter.readthedocs.io
BSD 2-Clause "Simplified" License
3.66k stars 632 forks source link

Bug: No exception when adding two autofilters #1102

Open rhshadrach-8451 opened 2 days ago

rhshadrach-8451 commented 2 days ago

Current behavior

In converting code from pandas to Polars, I neglected to see that Polars adds autofilters by default in write_excel. My prior code was adding autofilters manually via xlsxwriter. Thus switching to Polars, autofilters were being added twice. The Python code ran fine, but when opening the resulting workbook Excel needed to repair the file. The log stated:

Removed Feature: AutoFilter from /xl/tables/table1.xml part (Table)
Removed Feature: Table from /xl/tables/table1.xml part (Table)

Took me a bit to figure out what was going on.

Expected behavior

Not necessarily expected, but perhaps more desirable would be for xlsxwriter to detect the situation (assuming my diagnosis is correct - which I am not certain of) and raise an informative error message.

Not sure if this is feasible - if it isn't, then I thought just having this (closed) issue with the error message would possibly let others stumble upon a potential somewhat common case (switching from pandas to Polars).

Sample code to reproduce

import polars as pl
import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
df = pl.DataFrame({"a": [1, 1, 2], "b": [3, 4, 5]})
df.write_excel(workbook, worksheet="A")
ws = workbook.get_worksheet_by_name("A")
ws.autofilter(0, 0, len(df)-1, len(df.columns)-1)
workbook.close()

Environment

- XlsxWriter version: 3.2.0
- Python version: 3.10.15
- Excel version: 16.91
- OS: macOS 14.7

Any other information

This might be similar to:

OpenOffice and LibreOffice users

jmcnamara commented 2 days ago

Thanks. That is a bug.

The library doesn't allow 2 worksheet autofilters (since Excel doesn't allow it) but it doesn't prevent a worksheet autofilter and a table autofilter from overlapping (which it should do). I'll fix that.

Thanks for the detailed report.