PythonWin And Excel Problem

A

Andrea Gavana

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.Workbooks.Add()

xlsapp.Worksheets.Item(2).Delete()
xlsapp.Worksheets.Item(1).Delete()
sheet = wb.Sheets[0]
sheet.Name = "Data"
sheet.Range("A1:A10").Value = therand

excelfile = "C:/HelloWin32.xls"

wb.SaveAs(excelfile)
wb.Close()
xlsapp.Quit()

os.startfile(excelfile)


Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/
 
R

Roger Upole

Andrea Gavana said:
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.Workbooks.Add()

xlsapp.Worksheets.Item(2).Delete()
xlsapp.Worksheets.Item(1).Delete()
sheet = wb.Sheets[0]
sheet.Name = "Data"
sheet.Range("A1:A10").Value = therand

excelfile = "C:/HelloWin32.xls"

wb.SaveAs(excelfile)
wb.Close()
xlsapp.Quit()

os.startfile(excelfile)

Each row you insert into the Range need to be a
sequence:

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

Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top