如何使用openpyxl在工作簿中对Excel工作表/标签进行排序 [英] How can I sort excel sheets/tabs in workbook using openpyxl

查看:1497
本文介绍了如何使用openpyxl在工作簿中对Excel工作表/标签进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按字母顺序对工作簿中的标签/工作表进行排序.我正在使用 openpyxl 来处理工作表.

I need to sort the tabs/sheets in a workbook alpha- numerically. I am using openpyxl to manipulate the worksheet.

推荐答案

您可以尝试对workbook._sheets列表进行排序.

You can try to sort the workbook._sheets list.

workbook._sheets = sorted(workbook._sheets)

这可能会导致活动工作表(索引所指)无提示地更改,因此排序操作后应紧跟workbook.active = 0之类的东西.

This may result in the active worksheet (referred to by an index) to change silently so the sorting operation should be followed by something like workbook.active = 0.

我将首先使用测试数据进行尝试,因为直接访问所谓的私有" _sheets属性可能会弄乱事情并破坏您的数据.

I would try it first with test data as the direct access to the so-called "private" _sheets attribute might mess things up and corrupt your data.

根据下面查理·克拉克(Charlie Clark)的评论,使用所谓的私有"属性可能会损害模块的工作方式.如果要使其正确使用,请仔细阅读源代码以了解其含义,并为Workbook类创建子类,以添加用于进行排序的新方法.

As per the comment by Charlie Clark below, using so-called "private" attributes can compromise the way a module is working. If you want to make it proper read the source thoroughly to understand what it involves and subclass the Workbook class to add a new method that does the sorting.

...,请记住,更新openpyxl可能会导致您精巧的子类无法正常工作.您可能建议将排序方法包括在openpyxl代码中,以便对其进行充分维护.

... and bear in mind that updating openpyxl might result in your finely crafted subclass not working anymore. You may suggest to include the sorting method into the openpyxl code so that it is maintained adequately.

只要您喜欢,Python不会禁止您将计算机变成不听话的怪物.

Python will not forbid you to turn your computer into a disobedient monster as long as it is what you fancy.

我们都是成年人.

We are all consenting adults here.

Guido von Rossum

这篇关于如何使用openpyxl在工作簿中对Excel工作表/标签进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆