You may need to specify the full path when you specify the path to wb.save():

path (str, default None) – Full path to the workbook.

It will save the file and overwrite without prompting. From their docs:

>>> from xlwings import Workbook
>>> wb = Workbook()
>>> wb.save()
>>> wb.save(r'C:\path\to\new_file_name.xlsx')
Answer from aneroid on Stack Overflow
🌐
Blogger
htykuut.blogspot.com › 2019 › 01 › xlwings-save-and-close.html
xlwings: Save and Close
January 5, 2019 - It will save the file and overwrite without prompting. From their docs: >>> from xlwings import Workbook >>> wb = Workbook() >>> wb.save() >>> wb.save(r'C:pathtonew_file_name.xlsx')
Discussions

How to get rid of open excel windows on xlwings?
Here's what works for me: import xlwings as xw wb = xw.Book(filepath) #Changes to book made here. wb.save() wb.app.quit() More on reddit.com
🌐 r/learnpython
3
3
February 28, 2022
XLwings save fail
I suspect it's a formatting error on your path strings. You have an extra ' character. Try changing your path to path = r'C:\\Users\\myname\\OneDrive\\Documents\\BBGF\\Dashboard\\' also, I find it helpful to finish things with a wb.close() if needed More on reddit.com
🌐 r/learnpython
5
5
October 8, 2020
Frequent 'xlwings' Questions - Stack Overflow
when I execute a an Xlwings function I can save and close the workbook. But I cannot close Excel 2016 anymore. Is this a known issue? More on stackoverflow.com
🌐 stackoverflow.com
python - Xlwings won't close book after saving - Stack Overflow
I am currently trying to use xlwings to open a book and update it's links, then save and close. More on stackoverflow.com
🌐 stackoverflow.com
May 20, 2020
🌐
Byu
et.byu.edu › ~treedoug › _pages › teaching › ChEn263 › Lectures › Lec23-XLWings_handout.pdf pdf
XLWings Handout What is xlwings?
import xlwings as xw · Manipulating a Workbook · • Open a workbook · wb · = xw.books.open( ' data.xlsx ' ) • Save and Close a workbook · wb.save() wb.close() • Select a sheet from a workbook · sheet = wb.sheets( ' Sheet1 ' ) • Create a new sheet ·
🌐
Reddit
reddit.com › r/learnpython › how to get rid of open excel windows on xlwings?
r/learnpython on Reddit: How to get rid of open excel windows on xlwings?
February 28, 2022 -

So I have done the following:

import xlwings as xw
app = xw.App()
wb = xw.Book(filepath)
#Changes to book made here.
wb.save()
app.quit() 
#also tried wb.close() and app.kill()

It still leaves all of the excel windows open without the workbooks inside. Is there any way to close all of the empty excel windows?

🌐
Reddit
reddit.com › r/learnpython › xlwings save fail
r/learnpython on Reddit: XLwings save fail
October 8, 2020 -

I have created some dataframes that I want to save to different sheets in an excel workbook. This is a process I will run daily and I want the file name to reflect the time the workbook was generated/saved.

I discovered XLwings and it seems to work really well for what I want to do except for one big problem. I can't get the workbooks to save.

I am generating a workbook name and path using the following code.

from datetime import datetime 
import xlwings as xw

#Set the name of the dashboard we will create
now = datetime.now()
dt_string = now.strftime("%d_%m_%Y")
file_name = str("BBGF_Dash_"+ dt_string + ".xlsx'")

#Set the path 
path = "'C:/Users/myname/OneDrive/Documents/BBGF/Dashboard/"

#Create the path and name
full = str(path + file_name)

print(full)

The output is

'C:/Users/myname/OneDrive/Documents/BBGF/Dashboard/BBGF_Dash_08_10_2020.xlsx'

When I try to plug the variable 'full' which contains the path and filename into the save() function it fails.

wb = xw.Book()

wb.save(full)

Here is the error code

~\anaconda3\lib\site-packages\win32com\client\dynamic.py in SaveAs**(self, Filename, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution, AddToMru, TextCodepage, TextVisualLayout, Local, WorkIdentity)** com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'The file could not be accessed. Try one of the following:\n\n• Make sure the specified folder exists. \n• Make sure the folder that contains the file is not read-only.\n• Make sure the filename and folder path do not contain any of the following characters: < > ? [ ] : | or *\n• Make sure the filename and folder path do not contain more than 218 characters.', 'xlmain11.chm', 0, -2146827284), None)

What is weird is that if I take the output from print(full) and just plug it into save() manually, it works just fine.

I did make sure the folder is not read-only. I don't see any characters in my file path or file name that could be causing this.

I may be close to the 218 character limit but if I were over I would expect it to cause the error whether i use full or manually paste the output of full.

Would love to solve this one, it's one of the final steps on completing this project.

🌐
Stack Overflow
stackoverflow.com › questions › tagged › xlwings
Frequent 'xlwings' Questions - Stack Overflow
I am trying to find out how to save and close to an existing workbook using xlwings after writing in it: import xlwings as xw list_of_values = [1, 2, 3] workbook_path = 'abc.xlsx' wb = xw.Book(...
🌐
Stack Overflow
stackoverflow.com › questions › 76739566 › how-to-make-xlwing-wait-untill-saving-is-completed-to-close-the-file
python - How to make xlwing wait untill saving is completed to close the file - Stack Overflow
It goes like this: ´´´ import xlwings as xw; import time; app = xw.App(); with xw.Book("file path") as excel:; -excel_sheet = excel.sheets("sheet name"); -; -##This is just an example of changing something in the file; -excel_sheet.range("A1").value = 123; -excel.save(); app.quit; ´´´ I was having trouble formatting the code, so I used "-" to represent indentation and ";" to represent paragraphs
Find elsewhere
🌐
Xlwings
docs.xlwings.org › en › 0.25.3 › api.html
Python API - xlwings Documentation
local (bool, default False) – If True, saves files against the language of Excel, otherwise against the language of VBA. Not supported on macOS. corrupt_load (int, default xlNormalLoad) – Can be one of xlNormalLoad, xlRepairFile or xlExtractData. Not supported on macOS. ... Activates the book. ... steal_focus (bool, default False) – If True, make frontmost window and hand over focus from Python to Excel.
🌐
GitHub
github.com › xlwings › xlwings › issues › 1411
How to open and close an excel workbook in a python for loop using xlwings · Issue #1411 · xlwings/xlwings
September 6, 2020 - I need to open and close the same workbook in a python for loop without necessarily saving the workbook. I tried the following in xlwings: import xlwings as xw for i in range(5): print(i) book = xw.Book() book.app.quit() However this wil...
Author: xlwings
🌐
GitHub
github.com › xlwings › xlwings › issues › 1619
Not able to close the excel file that is open by xlwings · Issue #1619 · xlwings/xlwings
June 11, 2021 - import xlwings as xw def write_to_excel(sheetname,df1,df2,df3,df4,df5) wb = xw.Book(r"filepath/file.xlsx") ws = wb.sheets(sheetname) ws.cells(row,col).options(index=False, header=False).value = df1 ws.cells(row,col).options(index=False, header=False).value = df2 ws.cells(row,col).options(index=False, header=False).value = df3 ws.cells(row,col).options(index=False, header=False).value = df4 ws.cells(row,col).options(index=False, header=False).value = df5 wb.save() wb.close()
Author: xlwings
Top answer
1 of 5
16

Here are the two methods that work for me, the first if you are creating a workbook:

import xlwings as xw
app = xw.apps.add()
wb = xw.Book()     
wb.close()
app.quit()

Here is an updated answer for 2024 how I got it to work after the app stopped closing the workbook after being loaded from a specific path:

import xlwings as xw
path = [WorkbookPath]
wb = xw.Book(path) 
wb.save(path)
app = wb.app
wb.close()
app.kill()

Note it only works if you invoke app.kill() and not app.quit(). Also different syntax with how the app is invoked directly from the workbook.

2 of 5
2

Updated

You can use wb.app.quit() when you want to close Excel and the associated workbook. Assume that wb is your workbook. Keep in mind that wb.app.quit() does't work if you used wb.close() before wb.app.quit(). Here's an example:

import xlwings as xw

path = r"test.xlsx"
wb = xw.Book(path)
wb.app.quit()

But also consider to open and close workbooks by using with xw.App() as app (since version 0.24.3), I can only recommend it:

import xlwings as xw

with xw.App() as app:
    wb = xw.Book("test.xlsx")

    # Do some stuff e.g.
    wb.sheets[0]["A1"].value = 12345

    wb.save("test.xlsx")
    wb.close()

The with statement ensures proper acquisition and release of resources. The with statement prevents, when an error occurs before closing Excel properly, the problem that Excel stays open and has possibly hidden excel processes left over in the background (because of xw.App(visible=False), if it is used). The with statement has also the advantage that you don't need app.quit() anymore, because Excel closes anyway at the end of the with block. But wb.close() at the end of the with block is usable (but not necessary) – it achieves that the next time you open Excel, Excel will not display the message that Excel has recovered data that you may want to keep (as explained here).


As a side note, I had a situation where app.quit() did not work. In this case I used app.kill() instead.

🌐
Xlwings
docs.xlwings.org › en › latest › api › book.html
Book - xlwings Documentation
import xlwings as xw def my_macro(): wb = xw.Book.caller() wb.sheets[0].range('A1').value = 1 · To be able to easily invoke such code from Python for debugging, use xw.Book.set_mock_caller(). Added in version 0.3.0. ... Closes the book without saving it. Added in version 0.1.1. ... Flushes all pending actions to Excel and the Output pane.