[使用XLWINGS包装]
我试图打开一个Excel电子表格(现有),将数据写入指定的单元格,然后使用workbook.save()函数将文件保存为新文件名(以免覆盖)
现在我目前有:
app = xw.App(visible = False)
wbMain = wx.Book('Example.xlsm') #Open premade excel file
sheetMain = wbMain.sheets['Example']
sheetMain.range('A1').value = 'Test' #Modify value of cell A1
wbMain.save('NewFileName.xlsm') #Save to new filename
wbMain.close() #Close current excel sheet
app.kill() #Kill program (may not work)
(对不起,如果我的格式关闭的话)
这是我的追踪记录:
Traceback (most recent call last):
File "C:\Users\USER\Desktop\test_proj\main.py", line 34, in <module>
wbMain.save(NewFileName.xlsm)
----
File "C:\Users\USER\AppData\Local\Programs\Python\Python35\lib\site-
packages\xlwings\main.py", line 688, in save
return self.impl.save(path)
---
File "C:\Users\USER\AppData\Local\Programs\Python\Python35\lib\site-
packages\xlwings\_xlwindows.py", line 472, in save
self.xl.SaveAs(os.path.realpath(path))
----
File "C:\Users\USER\AppData\Local\Programs\Python\Python35\lib\site-
packages\xlwings\_xlwindows.py", line 63, in __call__
v = self.__method(*args, **kwargs)
---
File "<COMObject Open>", line 7, in SaveAs
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft
Excel', 'SaveAs method of Workbook class failed', 'xlmain11.chm', 0,
-2146827284), None)
我曾尝试查找XLWings文档和其他各种stackoverflow线程,但似乎没有一个提供有效的解决方案。以前有人遇到过同样的问题吗?
答案 0 :(得分:0)
我遇到了同样的问题-原来使用wbMain.save('C:/xxx/NewFileName.xlsm')中的完整路径为我工作。
向@aneroid求助!看到这里:xlwings: Save and Close