PythonWin And Excel Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrea Gavana

    #1

    PythonWin And Excel Problem

    Hi All,

    I am having some problems in running a very simple python script,
    which prints some numbers in an Excel spreadsheet. The numbers are
    stored in a list. I know that the numbers are different (random
    generated), but when I open the Excel file I get a column of data with
    all the numbers equal to the first item in the python list.
    I attach a very simple script that reproduces the problem.

    I am using Python 2.5, PythonWin build 210, Windows XP. Am I missing
    something? Thank you for every hint.

    import os
    import random

    from win32com.client import Dispatch

    therand = []
    for ii in xrange(10):
    therand.append( random.random() )

    xlsapp = Dispatch("Excel .Application")
    wb = xlsapp.Workbook s.Add()

    xlsapp.Workshee ts.Item(2).Dele te()
    xlsapp.Workshee ts.Item(1).Dele te()
    sheet = wb.Sheets[0]
    sheet.Name = "Data"
    sheet.Range("A1 :A10").Value = therand

    excelfile = "C:/HelloWin32.xls"

    wb.SaveAs(excel file)
    wb.Close()
    xlsapp.Quit()

    os.startfile(ex celfile)


    Andrea.

    "Imaginatio n Is The Only Weapon In The War Against Reality."

  • Roger Upole

    #2
    Re: PythonWin And Excel Problem

    "Andrea Gavana" <andrea.gavana@ gmail.comwrote:
    Hi All,
    >
    I am having some problems in running a very simple python script,
    which prints some numbers in an Excel spreadsheet. The numbers are
    stored in a list. I know that the numbers are different (random
    generated), but when I open the Excel file I get a column of data with
    all the numbers equal to the first item in the python list.
    I attach a very simple script that reproduces the problem.
    >
    I am using Python 2.5, PythonWin build 210, Windows XP. Am I missing
    something? Thank you for every hint.
    >
    import os
    import random
    >
    from win32com.client import Dispatch
    >
    therand = []
    for ii in xrange(10):
    therand.append( random.random() )
    >
    xlsapp = Dispatch("Excel .Application")
    wb = xlsapp.Workbook s.Add()
    >
    xlsapp.Workshee ts.Item(2).Dele te()
    xlsapp.Workshee ts.Item(1).Dele te()
    sheet = wb.Sheets[0]
    sheet.Name = "Data"
    sheet.Range("A1 :A10").Value = therand
    >
    excelfile = "C:/HelloWin32.xls"
    >
    wb.SaveAs(excel file)
    wb.Close()
    xlsapp.Quit()
    >
    os.startfile(ex celfile)
    >
    Each row you insert into the Range need to be a
    sequence:

    therand.append( (random.random( ),))

    Roger




    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

    Comment

    Working...