Newbie: returning dynamicly built lists (using win32com)

Steve Holden steve at holdenweb.com
Tue Jun 6 13:26:27 EDT 2006


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>]
> 
That's not an error, that's a list containing a single Python COM object.

> 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!!!
> 
I suspect that you need to apply judicious conversions to string or 
numeric to grab the values of the cells you are interested in, 
unencumbered by the COM wrappings.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list