append new data to an existing excel file

1,500 views
Skip to first unread message

Pyhton Cool

unread,
Jun 10, 2011, 6:38:03 PM6/10/11
to python-excel
I would like to collect new data and add it to an existing excel file.
But from the following website:
https://secure.simplistix.co.uk/svn/xlutils/trunk/xlutils/docs/copy.txt
it seems that the input excel file's name is different from the
output's file name. But if an input file name and an output file name
are the same, I get an error for the output file name:
IOError: [Errno 22] invalid mode ('wb') or filename:...

How can I solve my problem. Thanks.
PC

John Machin

unread,
Jun 10, 2011, 7:22:22 PM6/10/11
to python...@googlegroups.com
Show your code and the full traceback. Tell us what versions of Python, xlrd, xlwt, and xlutils you are using on what platform.

Pyhton Cool

unread,
Jun 11, 2011, 9:07:46 AM6/11/11
to python-excel
Here is my code:

def read_write_Xecel(self):
rb = open_workbook('D:\projects\Myself\HelloPython\src
\simple.xls',on_demand=True,encoding_override="cp1252")
i = rb.sheet_by_index(0).nrows + 2
wb = copy(rb)
sheet1 = wb.get_sheet(0)
for x in self.transpose:
j = 1
row = sheet1.row(i)
for e in x:
row.write(j,e)
j = j + 1
i = i + 1;
wb.save('D:\projects\Myself\HelloPython\src\simple.xls')

I run the above program in Eclipse with Python v2.7.1. I think the
problem is "on_demand=True" in open_workbook(...). When I removed that
option, the problem is gone. So, I cannot use the less memory feature?
Thanks.
PC

John Machin

unread,
Jun 11, 2011, 7:23:21 PM6/11/11
to python...@googlegroups.com
On Saturday, 11 June 2011 23:07:46 UTC+10, Pyhton Cool wrote:
Here is my code:

    def read_write_Xecel(self):
        rb = open_workbook('D:\projects\Myself\HelloPython\src
\simple.xls',on_demand=True,encoding_override="cp1252")

Not part of the problem, but: Why do you think you need to use encoding_override?
 
        i = rb.sheet_by_index(0).nrows + 2
        wb = copy(rb)
        sheet1 = wb.get_sheet(0)
        for x in self.transpose:
            j = 1
            row = sheet1.row(i)
            for e in x:
                row.write(j,e)
                j = j + 1
            i = i + 1;
        wb.save('D:\projects\Myself\HelloPython\src\simple.xls')

I run the above program in Eclipse with Python v2.7.1. I think the
problem is "on_demand=True" in open_workbook(...). When I removed that
option, the problem is gone. So, I cannot use the less memory feature?

The problem is that you are trying to overwrite your input file while the xlrd.Book object still has an mmap.mmap object referring to that file. Do rb.release_resources()  [available but not documented in xlrd 0.7.1] once after you have finished with the input Book object but before wb.save(....) .
 
 
Reply all
Reply to author
Forward
0 new messages