0

I have a Python script that writes an Excel table. I want to sort the table by the Attribute Name

How the script works:

import openpyxl
from openpyxl import workbook
from openpyxl import load_workbook
from openpyxl.worksheet.table import Table

Data = [[A,20,1,1000],[B,20,1,1000],[A,20,1,900],[B,20,1,900],[A,20,1,1100],[B,20,1,1100]]

wb = openpyxl.Workbook()
ws = wb.active
ws.append(['Name','%','Mode','Value'])
for row in Data:
   ws.append(row)
table = Table(displayName = 'Results', ref ='A1:D7')
ws.add_table(table)

There must be something like:

table.sort('Name', upwards)

But I can't find the needed function.

Samuel Dion-Girardeau
  • 2,790
  • 1
  • 29
  • 37
Simon
  • 19
  • 1
  • 4

1 Answers1

0

This question has been answered here!

But this should help you:

from openpyxl import load_workbook

wb=load_workbook('NotSorted.xlsx')
ws1=wb.get_sheet_by_name('Mean')

ws1.auto_filter.add_sort_condition('J2:J21')

wb.save('Sorted.xlsx')
Swetsen
  • 50
  • 8
  • This doesnt help me because in the documentation of openpyxel it says: This will add the relevant instructions to the file but will neither actually filter nor sort. As stated in the answer your link directs me to. Still Ty for your time – Simon Nov 30 '21 at 10:29