简体   繁体   English

无法在XLWings上保存文件

[英]Unable to save file on XLWings

[USING XLWINGS PACKAGE] [使用XLWINGS包装]

I'm attempting to open an excel spreadsheet (existing), write data to specified cells, and then use the workbook.save() function to save the file to a new file name (as not to overwrite) 我正在尝试打开一个Excel电子表格(现有),将数据写入指定的单元格,然后使用workbook.save()函数将文件保存为新文件名(以免覆盖)

Right now I currently have: 现在,我目前有:

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)

(Sorry about my formatting if it's off) (对不起,如果我的格式关闭了)

Here is my traceback: 这是我的回溯:

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)

I've tried lookin at the XLWings documentation and various other stackoverflow threads but none seemed to offer a solution that worked. 我曾尝试查找XLWings文档和其他各种stackoverflow线程,但似乎没有一个提供有效的解决方案。 Has anyone had the same issue before? 有人遇到过同样的问题吗?

I had the same problem - turns out using the full path in wbMain.save('C:/xxx/NewFileName.xlsm') worked for me. 我有同样的问题-原来在wbMain.save('C:/xxx/NewFileName.xlsm')中使用完整路径对我有用。

Credits to @aneroid for the help! 感谢@aneroid的帮助! See here: xlwings: Save and Close 参见此处: xlwings:保存并关闭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM