[Tutor] Traversing Excel Columns

John Fouhy john at fouhy.net
Tue Sep 12 23:27:27 CEST 2006


On 12/09/06, Chris Hengge <pyro9219 at gmail.com> wrote:
> I'm not sure what internal blanks means.. but I'll take a stab and say
> "no", there are going to be NO blanks once I start reading the column
> unless there are no more values to read... null or "" would be fine for
> a stopping point.

Well, basically, I'm thinking you could say something like:

for i in itertools.count(1):
    if xlSht.Cells(1, i).Value in (None, ''):
        maxCol = i
        break

This should start at (1,1) (the top-left cell), and move right, until
it hits a blank cell, at which point it aborts.

You could do the same thing, but changing row instead of column, to
find the maximum column.

So, by "internal blanks", I meant blank cells with non-blank cells to
the right, or below.

Also, if the first row is not necessarily the longest, you would need
a bit more trickery.

> also, what is makepy.py? I'm still working through a couple books on
> python, so I haven't got all the tricks yet :]

makepy.py is a utility that comes with pythonwin. Basically it builds
python libraries corresponding to COM objects.  If you do this
(one-time only), your code should run faster.  Also, you will get
meaningful help(), and you can look at the code it produces to get a
quick list of all the available methods, and what arguments they
expect.

You can run it from the command line, or you can run it from the menu
of the pythonwin IDE.

-- 
John.


More information about the Tutor mailing list