Python win32com Excel - How to sort?

Hi

In Python, I am looking to drive  Excel with win32com ..

Seems straight forward ..but I do not know how to do a two column sort.

So, with this little code snippet that addes in some data, how would I select a5:d9 and sort on column A, and Column B?

import win32com.client as win32

# Load excel:
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True
wb = excel.Workbooks.Add()
ws = wb.Worksheets('Sheet1')
ws.Name = 'Sort Data'

# Add data:
ws.Cells(5,1).Value = "Dallas"
ws.Cells(5,2).Value = "Amy"
ws.Cells(5,3).Value = 12
ws.Cells(5,4).Value = 2450.99

ws.Cells(6,1).Value = "New York"
ws.Cells(6,2).Value = "Allen"
ws.Cells(6,3).Value = 69
ws.Cells(6,4).Value = 2182.50

ws.Cells(7,1).Value = "Atlanta"
ws.Cells(7,2).Value = "Bobby"
ws.Cells(7,3).Value = 122
ws.Cells(7,4).Value = 124.44

ws.Cells(8,1).Value = "New York"
ws.Cells(8,2).Value = "Julie"
ws.Cells(8,3).Value = 28
ws.Cells(8,4).Value = 375.00

ws.Cells(9,1).Value = "Dallas"
ws.Cells(9,2).Value = "David"
ws.Cells(9,3).Value = 67
ws.Cells(9,4).Value = 3002.25

# Sort ????

Open in new window


Please help!

Thx Scott
srbarker8Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
For better performance
1. suspend screen updating
excel.screenupdating = False
#your worksheet populating or processing statements here
excel.screenupdating = True

2. You should be able to assign an entire set (row) of cell values at a time.  For that matter, you should be able to assign an entire 2D (array) range of values in a single operation.  Although my article uses VBA as the code examples, it should work in Python
Fast Data Push to Excel:  http:A_2253.html

You can specify up to three columns in a sort operation.  If more than three columns, you would need to iterate the columns, in least-to-most significant order.  I will post a two-column example.
0
aikimarkCommented:
This is how I would invoke the sort method in VBA.  Now I need to translate these optional (and named) parameters into Python.
ws.Cells(1, 1).CurrentRegion.Sort key1:=ws.Cells(1, 1), key2:=ws.Cells(1, 2), header:=2

Open in new window

0
aikimarkCommented:
Please try this:
ws.Cells(1, 1).CurrentRegion.Sort(key1=ws.Cells(1, 1), key2=ws.Cells(1, 2), header=2)

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

aikimarkCommented:
I think this might be the way to write the row-at-a-time push of data
excel.screenupdating = False
rng = wb.Worksheets('Sheet1').range('A1:D1')
rng.value = ["Dallas", "Amy", 12, 2450.99]
rng.Offset(1).value = ["New York", "Allen", 69, 2182.50]
rng.Offset(2).value = ["Atlanta", "Bobby", 122, 124.440]
rng.Offset(3).value = ["New York", "Julie", 28, 375.00]
rng.Offset(4).value = ["Dallas", "David", 67, 3002.25]
excel.screenupdating = True

Open in new window

0
aikimarkCommented:
I tested this syntax:
import win32com.client as win32
# Load excel:
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True
wb = excel.Workbooks.Add()
ws = wb.Worksheets('Sheet1');
ws.Name = 'Sort Data'

excel.ScreenUpdating = False
rng = ws.Range('A1:D1');
rng.Value = ["Dallas", "Amy", 12, 2450.99]
rng.GetOffset(1).Value = ["New York", "Allen", 69, 2182.50]
rng.GetOffset(2).Value = ["Atlanta", "Bobby", 122, 124.440]
rng.GetOffset(3).Value = ["New York", "Julie", 28, 375.00]
rng.GetOffset(4).Value = ["Dallas", "David", 67, 3002.25]
excel.ScreenUpdating = True

Open in new window

0
srbarker8Author Commented:
Hi there,

Thanks for working on this...

I tried this line as you suggested but the translation is failing on the parameters..

error messageAny suggestions?
0
aikimarkCommented:
Try capitalizing the first letter of the parameter names.  I found that the win32com interface to the Excel objects, methods, and properties is case sensitive.
0
aikimarkCommented:
Here is a script that works.  I had to add the orientation parameter to the sort to get it to sort 'properly'
import win32com.client as win32
# Load excel:   or gencache.EnsureDispatch
excel = win32.Dispatch('Excel.Application')
excel.Visible = True
wb = excel.Workbooks.Add()
ws = wb.Worksheets('Sheet1');
ws.Name = 'Sort Data'

excel.ScreenUpdating = False
rng = ws.Range('A1:D1');
rng.Value = ["Dallas", "Amy", 12, 2450.99]
rng.GetOffset(1).Value = ["New York", "Allen", 69, 2182.50]
rng.GetOffset(2).Value = ["Atlanta", "Bobby", 122, 124.440]
rng.GetOffset(3).Value = ["New York", "Julie", 28, 375.00]
rng.GetOffset(4).Value = ["Dallas", "David", 67, 3002.25]
rc = ws.Cells(1, 1).CurrentRegion.Sort(Key1=ws.Cells(1, 1), Key2=ws.Cells(1, 2), Header=2, Orientation=1)
excel.ScreenUpdating = True

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
Here is an example of pushing all the data into Excel in one operation.  Notice that the target range is five rows tall, enough to hold all the data.
import win32com.client as win32
# Load excel:   or gencache.EnsureDispatch
excel = win32.Dispatch('Excel.Application')
excel.Visible = True
wb = excel.Workbooks.Add()
ws = wb.Worksheets('Sheet1');
ws.Name = 'Sort Data'

excel.ScreenUpdating = False
rng = ws.Range('A1:D5');
rng.Value = (["Dallas", "Amy", 12, 2450.99], ["New York", "Allen", 69, 2182.50],
             ["Atlanta", "Bobby", 122, 124.440], ["New York", "Julie", 28, 375.00],
             ["Dallas", "David", 67, 3002.25])
rc = ws.Cells(1, 1).CurrentRegion.Sort(Key1=ws.Cells(1, 1), Key2=ws.Cells(1, 2), Header=2, Orientation=1)
excel.ScreenUpdating = True

Open in new window

0
srbarker8Author Commented:
Thx a lot!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.