?
Solved

Python win32com Excel - How to sort?

Posted on 2014-07-25
10
Medium Priority
?
1,707 Views
Last Modified: 2014-07-28
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
0
Comment
Question by:srbarker8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 2
10 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 40221394
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
 
LVL 46

Expert Comment

by:aikimark
ID: 40221410
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
 
LVL 46

Expert Comment

by:aikimark
ID: 40221417
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
Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

 
LVL 46

Expert Comment

by:aikimark
ID: 40221448
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
 
LVL 46

Expert Comment

by:aikimark
ID: 40222887
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
 

Author Comment

by:srbarker8
ID: 40224632
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
 
LVL 46

Expert Comment

by:aikimark
ID: 40224646
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
 
LVL 46

Accepted Solution

by:
aikimark earned 2000 total points
ID: 40224704
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
 
LVL 46

Expert Comment

by:aikimark
ID: 40224742
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
 

Author Closing Comment

by:srbarker8
ID: 40224746
Thx a lot!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question