Why cant I freeze_panes on the xlsxwriter object pandas is creating for me?

2024/10/8 4:28:28

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.

Answer

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...

https://en.xdnf.cn/q/70162.html

Related Q&A

Python multiprocessing pipe recv() doc unclear or did I miss anything?

I have been learning how to use the Python multiprocessing module recently, and reading the official doc. In 16.6.1.2. Exchanging objects between processes there is a simple example about using pipe t…

How would you unit test this SQLAlchemy Core query/function?

Im working on learning how to unit test properly. Given this function...def get_user_details(req_user_id):users = sa.Table(users, db.metadata, autoload=True)s = sa.select([users.c.username,users.c.favo…

Retrieve wall-time in Python using the standard library?

How can I retrieve wall-time in Python using the standard library?This question, and this question would suggest that something like clock_gettime(CLOCK_MONOTONIC_RAW) or /proc/uptime are most appropr…

NLTK: Package Errors? punkt and pickle?

Basically, I have no idea why Im getting this error. Just to have more than an image, here is a similar message in code format. As it is more recent, the answer of this thread has already been mentione…

Is there a bit-wise trick for checking the divisibility of a number by 2 or 3?

I am looking for a bit-wise test equivalent to (num%2) == 0 || (num%3) == 0.I can replace num%2 with num&1, but Im still stuck with num%3 and with the logical-or.This expression is also equivalent …

Check image urls using python-markdown

On a website Im creating Im using Python-Markdown to format news posts. To avoid issues with dead links and HTTP-content-on-HTTPS-page problems Im requiring editors to upload all images to the site and…

How to unittest command line arguments?

I am trying to supply command line arguments to Python unittest and facing some issues. I have searched on internet and found a way to supply arguments asunittest.main(argv=[myArg])The issue is this wo…

different foreground colors for each line in wxPython wxTextCtrl

I have a multilinewx.TextCtrl()object which I set its forground and Background colors for writing strings.I need to write different lines with different colors ,wx.TextCtrl.setForgroundcolor()changes a…

Access deprecated attribute validation_data in tf.keras.callbacks.Callback

I decided to switch from keras to tf.keras (as recommended here). Therefore I installed tf.__version__=2.0.0 and tf.keras.__version__=2.2.4-tf. In an older version of my code (using some older Tensorfl…

How to unpickle a file that has been hosted in a web URL in python

The normal way to pickle and unpickle an object is as follows:Pickle an object:import cloudpickle as cpcp.dump(objects, open("picklefile.pkl", wb))UnPickle an object: (load the pickled file):…