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)
mouwsy
  • 1,457
  • 12
  • 20

1 Answers1

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.

mouwsy
  • 1,457
  • 12
  • 20