J T
J T

Reputation: 255

How to freeze pane in XLWings

I like to check how to freeze panes in XLWings.

Hope anyone familiar with XLWings can assist.

I have tried the below but given an error: pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)

app1 = xw.apps
wb = app1.active.books.active
wb.api.Windows(6).FreezePanes = True 

And I am not able to find other code online.

Upvotes: 4

Views: 2701

Answers (2)

Tom
Tom

Reputation: 36

In case anyone comes to this thread searching for a simple solution to freeze rows/columns of a python DataFrame exported to Excel, just use Pandas.to_excel method with the 'freeze_panes' parameter like this:

with pd.ExcelWriter("filename.xlsx", engine = "xlsxwriter") as writer:
    df.to_excel(writer, sheet_name = "sheet_name", freeze_panes = (rows, columns))

With rows and columns being the number of each to freeze.

This will do the trick.

Upvotes: 2

Felix Zumstein
Felix Zumstein

Reputation: 7070

You can do it as follows:

import xlwings as xw
wb = xw.books.active
active_window = wb.app.api.ActiveWindow
active_window.FreezePanes = False
active_window.SplitColumn = 0
active_window.SplitRow = 6
active_window.FreezePanes = True

Upvotes: 6

Related Questions