Move sheets position

4,449 views
Skip to first unread message

miguel vfx

unread,
Apr 19, 2016, 2:54:26 AM4/19/16
to openpyxl-users
Hello, I would like to know if it's possible to sort, move or re arrange the position of worksheets? Thank you.

Thomas Nygårds

unread,
Apr 19, 2016, 3:21:21 AM4/19/16
to openpyxl-users
I guess it can be done by reordering the elements in

wb._sheets

like so:
from openpyxl import Workbook
wb
=Workbook()
# wb.create_sheet("Sheet")  # Sheet already exists
wb
.create_sheet("Sheet2")
wb
.create_sheet("Sheet3")
wb
.create_sheet("SheetA")
wb
.create_sheet("ASheet")
wb
.save('book_original.xlsx')
myorder
=[3,2,0,1,4]
wb
._sheets =[wb._sheets[i] for i in myorder]
wb
.save('book_myorder.xlsx')
wb
._sheets.sort(key=lambda ws: ws.title)
wb
.save('book_alphabetical.xlsx')

Regards
Thomas

ruship...@gmail.com

unread,
Jun 23, 2017, 6:56:57 PM6/23/17
to openpyxl-users
Yes Thomas thanks for the share i worked for me.
And you are correct it works like this.
Thanks Thomas... :)
Reply all
Reply to author
Forward
0 new messages