[Tutor] recursion sort of

Magnus Lyckå magnus@thinkware.se
Sat May 31 07:16:04 2003


At 16:45 2003-05-30 -0500, Isaac Hall wrote:
>One suggestion I might make...If I were doing this, instead of copying the
>results into an excel spreadsheet/graph, I would use one of pythons
>graphical interfaces to create the plot.

This is a lot more to learn though...and if Excel files are
used commonly at a location, why not use them.

Using the Windows extensions for Python, it's possible to
make the python script start Excel, move the data and make
a graph.

A simpler version is to simply make the data formatted so
that copy/paste is easier.

Example:

 >>> import math
 >>> result = []
 >>> for x in range(1, 11):
...     l = math.log(x)
...     s = math.sin(x)
...     result.append((x, l, s))
...

Now we have a set of data as a list of ten tuples, where
each tuple contains (x, log(x), sin(x)) for x = 1 to 10.

 >>> for x, l, s in result:
...     print x, ',', l, ',', s
...
1 , 0.0 , 0.841470984808
2 , 0.69314718056 , 0.909297426826
3 , 1.09861228867 , 0.14112000806
4 , 1.38629436112 , -0.756802495308
5 , 1.60943791243 , -0.958924274663
6 , 1.79175946923 , -0.279415498199
7 , 1.94591014906 , 0.656986598719
8 , 2.07944154168 , 0.989358246623
9 , 2.19722457734 , 0.412118485242
10 , 2.30258509299 , -0.544021110889

You can also use the string formatting features in Python and write:

 >>> for row in result:
...     print "%i, %f, %f" % row
...
1, 0.000000, 0.841471
2, 0.693147, 0.909297
3, 1.098612, 0.141120
4, 1.386294, -0.756802
5, 1.609438, -0.958924
6, 1.791759, -0.279415
7, 1.945910, 0.656987
8, 2.079442, 0.989358
9, 2.197225, 0.412118
10, 2.302585, -0.544021

This is all in the Python Tutorial. (Right?)

This you can easily paste into Excel. It will end up in one column,
but if you open the "Data" menu, and select "Text to Columns", you
just need to follow the wizard to get it into three columns. From
there, I'm sure you can make a graph.

But if you do this more than twice, it's boring to use that Wizard.
And you don't have to... Avoiding copy/paste/wizard means that you
need the win32all package (it's linked from the Python for Windows
web page) or ActiveState's ActivePython.

(I'd suggest that you save any currently open Excel files before you
try this.)

 >>> import win32com.client
 >>> xl = win32com.client.Dispatch("Excel.Application")
 >>> xl.Visible = 1
 >>> xl.Workbooks.Add()
<COMObject Add>
 >>> for x, l, s in result:
...     xl.Cells(x, 1).Value = x
...     xl.Cells(x, 2).Value = l
...     xl.Cells(x, 3).Value = s
...
 >>>

Your data should now be copied from your Python script to the Excel cells
you indicated with "xl.Cells(<row>, <column>)". Note that "x" doubles as
excel row variable and x-value in the maths in this particular example.

If you use data transfer to Excel often you will want to put the lines
above in a function that you can use again and again. That function might
have some convenient features. It should be able to place your data in
any location in a spread sheet, and in that case it should be able to use
a currently open spreadsheet as well as an new one like here. It could
also be able to open (and save?) an existing file, given a path. It could
also make the chart, or call an Excel macro which does that. But that's
too much for now...


--
Magnus Lycka (It's really Lyck&aring;), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program