gpt4 book ai didi

python - 为什么我不能从我的 Python 代码中 "save as"一个 Excel 文件?

转载 作者:太空狗 更新时间:2023-10-29 22:08:30 26 4
gpt4 key购买 nike

我有一个 Python 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)

保存方法完美运行,但是当我尝试调用 saveAs 方法时 - 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)

谁能解释一下发生了什么?

最佳答案

我发现(困难的方法)SaveAs 不支持斜杠 /
请尝试使用 saveAs("C:\\test.xlx") 代替。

关于python - 为什么我不能从我的 Python 代码中 "save as"一个 Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3730428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com