Hi, I am trying to write a script that takes in a user inputed Excel file and adds/deletes/edits a row and re-saves it. Because xlrd and xlwt dont work together I am forced to combine them together and copy the xlrd workbook to an xlwt workbook.
I am using python 2.6.1 on a windows xp service pack 2 machine
release versions of xlrd and xlwt

the issue when i attempt to save the file, i am hit with and IO error saying that my file path is wrong
here is the trace back of my error
-----------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\Lib\XLP3.py", line 397, in onEdit
self.EDIT(file_name)
File "C:\Python26\Lib\XLP3.py", line 377, in EDIT
new_book.save(new_book_path.__str__())
File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 501, in
save
doc.save(filename, self.get_biff_data())
File "C:\Python26\lib\site-packages\xlwt\CompoundDoc.py", line 507,
in save
f = open(file_name_or_filelike_obj, 'wb')
IOError: [Errno 22] invalid mode ('wb') or filename: 'testy.xls'
-------------------------------------------------------------------------------

i have tired useing just the name of the file (testy) and the complete path (C:\...........\testy.xls) and sometimes it works and other times I get the error


if it is at all useful, the code is given below
'''
EDIT: edits self.BOOK and saves it.
self.BOOK Book curently being worked on
self.reversePriner Changes the data in the txt box self.edit
into a usable format
self.addRow contains the row to be edited
'''

def EDIT(self,new_book_path):
bk = self.BOOK.sheet_by_index(0)
new_book = xlwt.Workbook()
new_sheet = new_book.add_sheet(bk.name)
self.reversePrinter(self.edit.GetValue())
target_row = self.addrow.GetValue()

for rowx in range(bk.nrows):
if(rowx == float(target_row)):
source = self.result
else:
source = bk.row_values(rowx)

for colx, value in enumerate(source):
new_sheet.write(rowx, colx, value)
new_book.save(new_book_path)

(currently new_book_path is passed as a string either 'testy.xls' or 'C:\Documents and Settings\pastesa\Desktop\XLedit\testy.xls')

if there is anymore information needed i will be happy to give it.

thanks

Try to save the file to the path. Maybe you don't have "enough" backslash...

def test_path(p):
    f=open(p,"w")
    f.write("test\n")
    f.close()

i changed the input a little,

IOError: [Errno 22] invalid mode ('wb') or filename: u'C:\\Documents and Settings\\pastesa\\Desktop\\XLedit\\testy.xls'

is that enough backslashes? i tried printing the file path also and go this

C:\Documents and Settings\pastesa\Desktop\XLedit\testy.xls

Can you run the test code I have given before?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.