Search code examples
pythonxlwings

unable to save workbook in xlwings


Unable to save a workbook using xlwings. Below is the code, loc contains the excel file name

import xlwings as xw
from xlwings import Book
print(xw.__version__)
app = xw.App(visible=False)
wb = Book(loc)
wb.sheets['crosst'].range('AJ2:CH9999').value=''

wb.save(loc)
app.quit()

below is the response, but when I check the Excel file, the contents are not cleared?

0.24.6

Process finished with exit code 0

Solution

  • Save the file under a new name, not under the same name.

    import xlwings as xw
    from xlwings import Book
    print(xw.__version__)
    app = xw.App(visible=False)
    wb = Book(loc)
    wb.sheets['crosst'].range('AJ2:CH9999').value=''
    
    wb.save(loc1)
    app.quit()
    

    where loc1 is another name.

    Ensure also that your cells are correct.