Newbie: returning dynamicly built lists (using win32com) (SOLVED)

Ransom gyows at sbcglobal.net
Tue Jun 6 14:52:39 EDT 2006


Thanks folks! I had just gotten myself into a blind rut, apparently.
Adding the .Value attribute to the com object does strip all the other
messaging returning from Excel so I could then populate my list and
return out of the function normally.

I had tried that earlier, but had used the .Value attribute incorrectly
( Cells.Value(x,y) instead of Cells(x.y).Value.

Cheers,
G




Ransom wrote:
> Very newb here, but my question will hopefully be obvious to someone.
>
> Code:
>
> import string
> from win32com.client import Dispatch
> docdir = 'E:\\scripts\\Python\\RSAutomation\\'
>
> def getOldData(testcases):
>
>     excel = Dispatch("Excel.Application")
>     excel.Workbooks.Open(docdir + 'FILE.xls')
>
>     # load and create list from file (testcases.csv)
>     for rsinput in testcases.xreadlines():
>
>         inputlist = string.split(rsinput, ',')
>
>
>         # iterate through and update spreadsheet input
>         cellx = range(3,51)
>         values = range(0,48)
>         for i,r in zip(cellx, values):
>
>             excel.ActiveSheet.Cells(i,2).Value = inputlist[r]
>
>         # TODO: read output from cell 32,6 into a tuple or list and
> then return list to __main__
>
>         [THIS IS WHERE I AM HAVING A PROBLEM]
>         print excel.ActiveSheet.Cells(32,6)   <--This prints properly
> as loop executes
>
>     excel.ActiveWorkbook.Close(SaveChanges=0)
>     excel.Quit()
>
> if __name__ == "__main__":
>     csv_testcases = open('arse_testcases.csv','r')
>     getOldData(csv_testcases)
>
> OK, so what is happening is that I am sending a list of data to an
> overly complicated spreadsheet that produces it's own output (in cell
> 32,6). As I loop through multiple test cases, the print statement
> calling into COM for the cell data seems to be printing out results
> just fine. But when I try and put the output from the spreadsheet into
> a dynamic list after the TODO section thusly:
>
>         outputlist = []
>         outputlist.extend(excel.ActiveSheet.Cells(32,6)
>         return outputlist
>
> I get an error like:
> [<win32com.gen_py.Microsoft Excel 9.0 Object Library.Range instance at
> 0x15450880>]
>
> I need to be able to return the dynamically generated built up by the
> responses from the spreadsheet lookup call (the exce.Activesheet
> thingy). Is there a better way to get this dynamically built list out
> of the funtion?
> 
> Thanks!!!




More information about the Python-list mailing list