Reputation: 99
I am opening an Excel-file wb with xlwings, doing some operations, and then trying to save it to a directory. The problem is that a normal xw.Book.save() throws me an occasional error.
For me to continue with this code, it is crucial that I can trust it not throwing the error.
The following code gives me the following error:
The code:
Some_data = r"C:\Users\FrodeWilkensen\OneDrive - Norfund\Frode Wilkensen\Some_data.xlsx" #filepath to some data
Original = r"C:\Users\FrodeWilkensen\OneDrive - Norfund\Frode Wilkensen\Original.xlsx" #filepath to another file
df = pd.read_excel(Some_data, engine = "openpyxl") #reading some_data, assigning as df
wb = xw.Book(Original) #assigning original workbook as xlwings wb
ws = wb.sheets["dump"] #the sheet called "dump" assigned as ws
ws["B2"].options(pd.DataFrame, index=False, header=False, expand="table").value = df #some operations
wb.save(r"C:\Users\FrodeWilkensen\OneDrive - Norfund\Frode Wilkensen\dumped.xlsx") #want to save it in same directoy
#gets thrown an error, shown below.
The error:
---------------------------------------------------------------------------
com_error Traceback (most recent call last)
Input In [49], in <cell line: 11>()
7 ws = wb.sheets["dump"] #the sheet called "dump" assigned as ws
8 ws["B2"].options(pd.DataFrame, index=False, header=False, expand="table").value = df #some operations
---> 11 wb.save(r"C:\Users\FrodeWilkensen\OneDrive - Norfund\Frode Wilkensen\dumped.xlsx") #want to save it in same directoy
12 #gets thrown an error, shown below.
13 wb.app.kill()
File ~\anaconda3\lib\site-packages\xlwings\main.py:874, in Book.save(self, path)
872 path = utils.fspath(path)
873 with self.app.properties(display_alerts=False):
--> 874 self.impl.save(path)
File ~\anaconda3\lib\site-packages\xlwings\_xlwindows.py:582, in Book.save(self, path)
580 alerts_state = self.xl.Application.DisplayAlerts
581 self.xl.Application.DisplayAlerts = False
--> 582 self.xl.SaveAs(os.path.realpath(path), FileFormat=file_format)
583 self.xl.Application.DisplayAlerts = alerts_state
File ~\anaconda3\lib\site-packages\xlwings\_xlwindows.py:70, in COMRetryMethodWrapper.__call__(self, *args, **kwargs)
68 while True:
69 try:
---> 70 v = self.__method(*args, **kwargs)
71 if isinstance(v, (CDispatch, CoClassBaseClass, DispatchBaseClass)):
72 return COMRetryObjectWrapper(v)
File C:\Users\FRODEW~1\AppData\Local\Temp\gen_py\3.9\00020813-0000-0000-C000-000000000046x0x1x9.py:46807, in _Workbook.SaveAs(self, Filename, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution, AddToMru, TextCodepage, TextVisualLayout, Local, WorkIdentity)
46804 def SaveAs(self, Filename=defaultNamedNotOptArg, FileFormat=defaultNamedNotOptArg, Password=defaultNamedNotOptArg, WriteResPassword=defaultNamedNotOptArg
46805 , ReadOnlyRecommended=defaultNamedNotOptArg, CreateBackup=defaultNamedNotOptArg, AccessMode=1, ConflictResolution=defaultNamedOptArg, AddToMru=defaultNamedOptArg
46806 , TextCodepage=defaultNamedOptArg, TextVisualLayout=defaultNamedOptArg, Local=defaultNamedOptArg, WorkIdentity=defaultNamedOptArg):
> 46807 return self._oleobj_.InvokeTypes(3174, LCID, 1, (24, 0), ((12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (3, 49), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17)),Filename
46808 , FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup
46809 , AccessMode, ConflictResolution, AddToMru, TextCodepage, TextVisualLayout
46810 , Local, WorkIdentity)
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'SaveAs method of Workbook class failed', 'xlmain11.chm', 0, -2146827284), None)
As you see at the end of the error, it says SaveAs method of Workbook class failed - which is a VBA method error? Do anyone have any suggestions?`
In advance, thank you :)
Upvotes: 3
Views: 1437
Reputation: 1
Yes agree. Do not use a folder that is in OneDrive to store the resulting sheets. It worked fine once I changed the storing folder
Upvotes: 0
Reputation: 342
Although this question is a little old, I have had exactly the same issues today, even after making sure I am using a full path to the wb.save()
method, as detailed in this answer.
I believe that the problem in this instance is caused by saving to a synced OneDrive folder, as shown by the example path in your question. Although I am mostly guessing, I think the issues is caused by the synchronisation of the new file in OneDrive, and the underlying SaveAs call having problems with finding the local path.
I solved this by pausing the syncing of my OneDrive while I am running the script that saves to the folder.
Upvotes: 0