Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move worksheet within a workbook to the end [duplicate]

With xlwings, I am trying to move a worksheet within a workbook to the end. For example, a workbook contains a collection of the following sheets:
Sheet1, Sheet2, Sheet3

How can I move Sheet1 after Sheet3 in order to get the following order?
Sheet2, Sheet3, Sheet1

If I use ws1.api.Move(Before=ws3.api) with the Before parameter the line works as expected, but it doesn't with the After parameter. See example code:

import xlwings as xw

wb = xw.Book("test.xlsx")

ws1 = wb.sheets['Sheet1']
ws3 = wb.sheets['Sheet3']

ws1.api.Move(After=ws3.api)
like image 845
mouwsy Avatar asked Oct 19 '25 04:10

mouwsy


1 Answers

I found the solution myself, this question is already answered here. You have to add None before the After parameter:

import xlwings as xw

wb = xw.Book("test.xlsx")

ws1 = wb.sheets['Sheet1']
ws3 = wb.sheets['Sheet3']

ws1.api.Move(None, After=ws3.api)
# Or: ws1.api.Move(Before=None, After=ws3.api)

Strictly speaking, the question seems to be more related to pywin32 than to xlwings, because apparently .api accesses the underlying pywin32 objects, as described here.

like image 167
mouwsy Avatar answered Oct 21 '25 16:10

mouwsy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!