PyExcelerator: How does one close a file?

T

tkpmep

I use PyExcelerator to write data into Excel files, and repeatedly call
the function writeData with different filenames fn from my main
routine. Unfortunately, only one file - the last one - is created. The
problem seems to lie with the fact that I do not close the existing
file before calling the function again, and the existing file is
therefore just saved with a new name. Is there a way to close a
PyExcelerator Excel file so that each call to the function results in a
new file? There is no close() method as far as I can tell.

def writeData(fn, Data):
"""Write the data into an Excel file named fn using PyExcelerator
Data is a list of real numbers"""
w=PyExcelerator.Workbook()
ws = w.add_sheet("Sheet1")
for i in range(len(Data)):
ws.write(i, 0, Data)
w.save(fn)

Thanks in advance

Thomas Philips
 
J

John Machin

I use PyExcelerator to write data into Excel files, and repeatedly call
the function writeData with different filenames fn from my main
routine. Unfortunately, only one file - the last one - is created. The
problem seems to lie with the fact that I do not close the existing
file before calling the function again,

If you needed to close a file that was opened in another module, that
would be a very peculiar API design. You don't need to.
and the existing file is
therefore just saved with a new name.

Your function creates a new instance of the Workbook class, as it
should. The instance is abandoned when your function exits. One would
not expect any carry-over between instances.

Is there a way to close a
PyExcelerator Excel file so that each call to the function results in a
new file? There is no close() method as far as I can tell.

def writeData(fn, Data):
"""Write the data into an Excel file named fn using PyExcelerator
Data is a list of real numbers"""
w=PyExcelerator.Workbook()

Last time I looked, the name was pyExcelerator -- that's a lower-case
"p" at the front. How come you didn't get an exception here (or on
earlier import)? Have you posted the actual code you executed???
ws = w.add_sheet("Sheet1")
for i in range(len(Data)):
ws.write(i, 0, Data)
w.save(fn)


pyExcelerator is open-source; you should read it. According to my
reading of the version 0.6.3 source (which version are you using?), the
Workbook.save method calls the CompoundDoc.save method, which opens a
physical file with the supplied name, writes to it, and closes it.
AFAICT, there is no hint of anything that could cause the symptoms you
describe.

I did a quick test using the following code and your function (with
appropriate module name) on version 0.6.3. It worked as expected (Python
2.4.3 on Windows XP). Which Python version are you using? What OS?

for k in range(3):
writeData("zzz%d.xls" % k, range(k, k+10))

Perhaps the problem is closer to home; try inserting
print repr(fn)
before each call to your function, and
os.system("ls -l " + fn) # or "dir " + fn, or whatever it takes
after each call.

HTH,
John
 
T

tkpmep

John,

I had spelled PyExcelerator with a capital P, but it worked just fine.
I then checked the directory it was installed in, and found it read
C:\Python24\Lib\site-packages\PyExcelerator. As soon as I changed the
directory name to C:\Python24\Lib\site-packages\pyExcelerator, my
programs stopped working! I then changed PyExcelerator to pyExcelerator
in my programs and they worked alll over again.

After much playing around, I discovered that I had incorrectly indented
the call to writeData, so that it ran only once, after a loop was
exited, and not for every iteration through the loop.

Thanks

Thomas Philips
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top