jmcnamara / XlsxWriter

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

feature request: arbitrary attributes hooks #1083

Closed jwhitaker-gridcog closed 3 months ago

jwhitaker-gridcog commented 3 months ago

Feature Request

Hey, thanks so much for your work maintaining this library.

I'm looking for the ability to be able to hook and add arbitrary attributes when writing cells, to allow for the case where I need to do something that XlsxWriter hasn't implemented. Specifically in this case I've hit cell styles - I understand you don't wish to support it and don't blame you! However, I'd be very happy if xlsxwriter had hooks to let me set "xfId" on cells: that way I'm given enough rope to hang myself, and you are not stuck needing to support every feature under the sun.

Would you accept a PR to this effect? Something along the lines of

cell_string_tuple = namedtuple("String", "string, format, extra_attributes")
cell_number_tuple = namedtuple("Number", "number, format, extra_attributes")

...

def _write_cell(self, row, col, cell):
  ...
  attributes = [("r", cell_range)]
  if cell.extra_attributes:
    attributes += cell.extra_attributes
  ...

...

def write_datetime(self, row, col, date, cell_format=None, extra_attributes: list[tuple[str, str]] = []):
  ...
  self.table[row][col] = cell_datetime_tuple(number, cell_format, extra_attributes)

Then I, as a user who's happy to venture into unsupported please-break-my-excel territory, am able to do

wb.write_datetime('A1', dt(2020,1,1), extra_attributes=[("xfId": "1234")])
jwhitaker-gridcog commented 3 months ago

Alternatively I'd also be happy to contribute a PR to implement cell styles, but again only if you want to accept that contribution.

jmcnamara commented 3 months ago

Specifically in this case I've hit cell styles

Could you say a bit more about your use case for styles.

I understand you don't wish to support it

I would describe myself as reticent in relation to cell styles. They have a complex interaction with non-style format elements so I have always avoided it, apart from the Hyperlink style. Also, this isn't a frequently requested feature. In the 10 years of XlsxWriter (and the other language variants) it has only been requested a handful of times.

I'm looking for the ability to be able to hook and add arbitrary attributes when writing cells

I suspect this wouldn't be useful in any meaningful way. The Excel file format (unfortunately from an implementor point of view) has a lot of reference ids to data structures in companion files so it is rarely enough to add in extra attributes.

jwhitaker-gridcog commented 3 months ago

Could you say a bit more about your use case for styles.

Our company is generating reports for users. We are currently using openpyxl but wish to write with xlsxwriter, for performance reasons, in particular writing very large data frames.

We want to control the look of our reports with a template file. This means aesthetic changes can be made by non-technical users. We have a basic in-house templating system that achieves this, built around openpyxl.

We are using, and want to continue to use, cell styles in templates. There are two reasons for this:

I suspect this wouldn't be useful in any meaningful way.

I understand the level of plumbing needed to do what I want manually. Most of it I reckon could be done by monkey patching your XML writer classes (Style). However it's harder to hook into the cell writing system. It's likely though that there would likely be follow up requests for some hooks into other places. It nearly makes me wish your intermediate representation was a little closer to XML, so that way my path to hooking into things could just be modifying your internal state. I know consumption of all this would be unsupported use of private API, but it still beats going down the internal fork path.

jmcnamara commented 3 months ago

Thanks for the explanation.

  • I don't want template authors to need to think much about the small subset of excel features they can use. If they want to set up their template by clicking the 'good' cell style then this should just work for them.

Are you only using the standard styles or are you also redefining some of them or creating new styles?

jwhitaker-gridcog commented 3 months ago

Some of both!

On Thu, 1 Aug 2024, 08:49 John McNamara, @.***> wrote:

Thanks for the explanation.

  • I don't want template authors to need to think much about the small subset of excel features they can use. If they want to set up their template by clicking the 'good' cell style then this should just work for them.

Are you only using the standard styles or are you also redefining some of them or creating new styles?

— Reply to this email directly, view it on GitHub https://github.com/jmcnamara/XlsxWriter/issues/1083#issuecomment-2261596142, or unsubscribe https://github.com/notifications/unsubscribe-auth/BFBS42LU2CKW7YQO2N2XFYLZPFSW7AVCNFSM6AAAAABLXWOCZOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENRRGU4TMMJUGI . You are receiving this because you authored the thread.Message ID: @.***>

jmcnamara commented 3 months ago

I am going to close this request since arbitrary attribute hooks is a non-runner as a feature.

I will think about adding proper support for Styles at some point but currently it is in the unfavourable "Low number of requests + High difficulty" quadrant.

jwhitaker-gridcog commented 3 months ago

Makes sense, thanks again.