通过在openpyxl中的单元集合进行sorting
我正在阅读一个xlsx文件,像这样,
import openpyxl book = openpyxl.load_workbook('test.xlsx') sheet = book.get_sheet_by_name(sheets['DATA']) allCells = sheet.get_cell_collection() print allCells
allCells是openpyxl.cell.Cell对象的无序列表。 allCells看起来像,
[<Cell Data.B4>, <Cell Data.A6>, <Cell Data.B6>, <Cell Data.A1>, <Cell Data.B5>, <Cell Data.A3>, <Cell Data.A2>, <Cell Data.A5>, <Cell Data.B1>, <Cell Data.B2>]
我想按列sorting这个列表,然后是行索引。 任何想法如何做到这一点?
您可以获取列表中每个单元格的行和列信息,如下所示:
import openpyxl wb = openpyxl.workbook.Workbook() ws = wb.worksheets[0] a=ws.cell('D6') a.row 6 a.column 'D'
现在只是按照你所希望的任何标准来分类。 以下是用于sorting的不同algorithm列表。 这篇文章介绍了如何根据对象的属性对列表进行sorting。 问题是什么? 我会相应地编辑答案。