Why can't I “save as” an Excel file from my Python code?

落爺英雄遲暮 提交于 2019-12-17 19:43:47

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/3730428/why-cant-i-save-as-an-excel-file-from-my-python-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!