代码之家  ›  专栏  ›  技术社区  ›  froadie

为什么不能从python代码中“另存为”Excel文件?

  •  5
  • froadie  · 技术社区  · 14 年前

    我有一条蟒蛇 ExcelDocument 类,它为读取/写入/格式化Excel文件提供了基本的方便方法,而我在看似简单的Python代码中遇到了一个奇怪的错误。我有一个储蓄和 saveAs 方法:

    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)
    

    save方法非常有效,但是当我尝试调用 萨维斯 方法- myExcelObject.saveAs("C:/test.xlsx") -我得到以下错误:

    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)
    

    有人能解释发生了什么事吗?

    1 回复  |  直到 14 年前
        1
  •  14
  •   Nick Dandoulakis    14 年前

    我发现(困难的)是 SaveAs 不支持斜线 / .
    尝试 saveAs("C:\\test.xlx") 相反。