Move worksheet within a workbook to the end [duplicate]
Asked Answered
C

1

4

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)
Confound answered 13/11, 2020 at 12:30 Comment(0)
C
3

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.

Confound answered 14/11, 2020 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.