How do I save a new workbook in Python using xlwing while it gives traceback error

Viewed 41

I have a simple code to create a new book and then after I want to fill it with data, but .save doesn't work

import xlwings as xw

app = xw.App(visible=False)
wb = xw.Book()
wb.save(r'D:\XX\report.xlsx')
wb.close()
app.quit()

and this is the error:

Traceback (most recent call last):
  File "MyPython.py", line 5, in <module>
    wb.save(r'D:\XX\report.xlsx')
  File "C:\Python38\lib\site-packages\xlwings\main.py", line 1065, in save
    self.impl.save(path, password=password)
  File "C:\Python38\lib\site-packages\xlwings\_xlwindows.py", line 829, in save
    self.xl.SaveAs(
  File "C:\Python38\lib\site-packages\xlwings\_xlwindows.py", line 95, in __call__
    v = self.__method(*args, **kwargs)
  File "C:\Users\XX\AppData\Local\Temp\gen_py\3.8\00020813-0000-0000-C000-000000000046x0x1x9.py", line 46571, in SaveAs
    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
pywintypes.com_error: (-2147352573, 'Member not found.', None, None)

Here is my python version: Python 3.8.0 XLwings vesrion: xlwings-0.27.7

1 Answers

The issue is coming from the path that you give as argument to the function save(). You can see that your code works if you save in the folder where your script is running:

import xlwings as xw

app = xw.App(visible=False)
wb = xw.Book()
wb.save('report.xlsx')
wb.close()
app.quit()

If you want to save to specific folder, you can do like this:

import xlwings as xw

app = xw.App(visible=False)
wb = xw.Book()
wb.save('C:/Users/rcapron/report.xlsx')
wb.close()
app.quit()

Please note that I use / instead of \.

Related