7

I have an Python ExcelDocument class that provides basic convenience methods for reading/writing/formatting Excel files, and I'm getting a strange error in seemingly simple Python code. I have a save and saveAs method:

def save(self):
   ''' Save the file '''
   self.workbook.Save()

def saveAs(self, newFileName):
   ''' Save the file as a new file with a different name '''
   self.workbook.SaveAs(newFileName)

The save method works perfectly, but when I try to call the saveAs method - myExcelObject.saveAs("C:/test.xlsx") - I get the following error:

Traceback (most recent call last):
  File "C:\workspace\Utilities\src\util\excel.py", line 201, in <module>
    excel.saveAs("C:/test.xlx")
  File "C:\workspace\Utilities\src\util\excel.py", line 185, in saveAs
    self.workbook.SaveAs(newFileName)
  File "<COMObject Open>", line 7, in SaveAs
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u"Microsoft Office Excel cannot access the file 'C:\\//8CBD2000'. There are several possible reasons:\n\n\u2022 The file name or path does not exist.\n\u2022 The file is being used by another program.\n\u2022 The workbook you are trying to save has the same name as a currently open workbook.", u'C:\\Program Files\\Microsoft Office\\Office12\\1033\\XLMAIN11.CHM', 0, -2146827284), None)

Can anyone explain what is happening?

NorthCat
  • 9,643
  • 16
  • 47
  • 50
froadie
  • 79,995
  • 75
  • 166
  • 235
  • A few questions. Does the file already exist? Is the file already open? Where does the `save()` write the file to? – John Keyes Sep 16 '10 at 20:20

1 Answers1

19

I've found (the hard way) that SaveAs doesn't support slash /.
Try saveAs("C:\\test.xlx") instead.

Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
  • wow, that did it. thanks so much!! but why is it different than the save method? is there any way I can sanitize the input? I can't really trust all my users to know about this quirk... – froadie Sep 16 '10 at 20:30
  • 1
    @froadie, `SaveCopyAs` supports `/`, but `SaveAs` doesn't. Go figure. I just replace `/` with `\\` in the file names. – Nick Dandoulakis Sep 16 '10 at 20:33
  • 8
    Or use `saveAs(os.path.join("C:", "test.xlx"))`. The `os.path` library is chock full of platform-independent ways to handle filenames, paths, whatever. – Mike DeSimone Sep 16 '10 at 20:35
  • 1
    @froadie, try `os.path.normpath`. – Nathan Ernst Sep 16 '10 at 20:42
  • Incredible that this was the problem. Thanks a thousand times. – Philippe F Feb 01 '12 at 11:30
  • 1
    @MikeDeSimone As of python `2.7.9`, you actually have to use `os.path.join('C:', os.sep, 'test.xlx')`. or there will **NOT** be any slash between `C:` and `test.xlx`. See [this post](http://stackoverflow.com/a/2422864/2160440) for more detail. – wlnirvana Jan 07 '15 at 14:12
  • @NickDandoulakis, thanks a lot, your comment helped me finally fix the issue. I was using `/` in my code and searched numerous SO/google posts and could find nothing, until I landed here. – ParvBanks Dec 26 '18 at 11:22
  • It keeps throwing an error saying `saveAs` isn't a method, the only thing that works for me is `SaveCopyAs` but then I am left with a prompt box asking me to save the original file – mp252 Mar 19 '19 at 15:32
  • @mp252, try `SaveAs` instead of `saveAs` – Nick Dandoulakis Mar 19 '19 at 16:23
  • @NickDandoulakis I tried both but didn't work, I made a workaround, the workbook that I create, instead of creating a new one I open an excel file and then am able to use `save` – mp252 Mar 19 '19 at 16:29