为什么我不能“另存为”我的 Python 代码中的 Excel 文件?

发布于 2024-09-24 05:53:36 字数 1308 浏览 5 评论 0原文

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

任何人都可以解释发生了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2024-10-01 05:53:36

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文