trying to parse to Excel...

Chris Liechti cliechti at gmx.net
Thu May 23 15:50:37 EDT 2002


btjenkins at att.net (Ben) wrote in 
news:9013d0b4.0205231133.70b58257 at posting.google.com:

> I am trying to create a simple script that will open a file (based
> upon date) in a default directory and convert it to an Excel
> spreadsheet.  This file will contain hundreds of lines of text - each
> line will have approximately 18 items separated by commas.
> 
> This is the code I have developed so far:
> from win32com.client import Dispatch
> import string, sys
> 
> xlApp = Dispatch("Excel.Application")
> xlApp.Visible = 1
> xlApp.Workbooks.Add()
> 
> file=open("Test.txt", "r")
> lines=file.readlines()
> file.close()
> x=1
> y=1
> for line in lines:
>     parms=string.split(line, ",")    
>     parms[0]

this line above does nothing, just leave it away.

>     for i in xrange(18):        
>         xlApp.ActiveWorkbook.ActiveSheet.Cells(x,y).Value = parms[i]

why not use i as counter in .Cells(i,y) (and maybe rename it to x, read 
below)

>         y=y+1
>     x=x+1
>     y=1

is it because your reseting the y counter each time?
from your descripion below i would expect the use of x and y the other way 
round (x counts columns but in your code it counts the rows/lines from the 
file)

> The script finds the file, opens Excel and populates the first row. 
> BUT, it does not continue to loop through the file and populate the
> other rows.  I am relatively new to Python and would appreciate any
> advice that you may have.  Should you need additional information,
> please let me know.

i suppose you're learning python and do this for education... because excel 
could very well read your file directly.

chris


-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list