Why can't I freeze_panes on the xlsxwriter object pandas is creating for me?
Asked Answered
A

1

7

I have a class whose objects contain pandas dataframes (self.before, and self.after below) and a save() method which uses xlsxwriter to export the data (which has two worksheets, "before" and "after"). I'm trying to freeze panes (and later want to use conditional formatting too).

Even though I know you can't change the style of cells you've already written, these two operations are applied at the worksheet level and therefore should be settable later.

Here's the code:

def save():
    writer = pd.ExcelWriter(self.path, engine='xlsxwriter')
    self.before.to_excel(writer, "before")
    self.after.to_excel(writer, "after")

    for sht_name in writer.sheets:
        ws = writer.sheets[sht_name]
        ws.freeze_panes=(2,0)

    writer.save()

The panes of the worksheets that get saved are NOT frozen. I've used the method successfully when using xlsxwriter (without pandas), and I'm following the lead set by this pandas-charts documentation which appears to just retrieve the underlying xlsxwriter objects from the writer object and operate on those.

Do you have ideas about why I'm failing to get that result here.

If for whatever reason this can't be done, I can always retrieve the individual values of the table from the dataframe, of course, but that seemed excessive upon first glance.

Arrogant answered 24/8, 2014 at 20:35 Comment(0)
A
10

OK, found it after much self-torment:

the correct syntax is:

ws.freeze_panes(2,0)

I was setting a (new) attribute (probably overwriting the method) rather than calling the worksheet object's method.

Once I corrected this it worked.

Glad it was such as simple solution...

Arrogant answered 24/8, 2014 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.