I am using XlsxWriter to create an Excel file using a Python script.

When I re-run the script to update the file with new information, which I need to do often, I have to manually close the Excel file and reopen it to look at the new version of the spreadsheet.

Is there a way to automate this process: Close the excel file (not the application) using a terminal command and then re-open it.

I know how to open it, but don't know how to close a file.

  • i'm not sure if this is relevant but maybe a VBA macro is ok for you and maybe it can be called from the command line stackoverflow.com/questions/2050505/…
    – barlop
    Aug 15, 2015 at 11:54
  • Please see the edits. I have excel. That is what I am using currently to look at the file. @barlop I am on Mac OSX. I am not sure whether the link you gave would work for that. Sorry, forgot to include that information above.
    – user37473
    Aug 15, 2015 at 15:15

2 Answers 2


Looking at a post over on Stack Overflow, the answer was that the Workbook COM contains within it a Close() method. The code snippet from the mentioned post:

xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Open('New Workbook.xlsx')
# do some stuff
wb.Close(True) # save the workbook

Here's the necessary syntax rules from the Microsoft site:

expression .Close(SaveChanges, Filename, RouteWorkbook)
expression A variable that represents a Workbook object.

When it comes to OSX, apparently you need to use appscript

With some looking, I'm not overly familiar with appscript, you should be able to get to do what you are looking for by doing something like:

tell application "Microsoft Excel"
   close workbook 1 saving no
end tell

Finally, here is the documentation for the appscript package for Python:


  • I think this solution is for Windows. I am using Excel on Mac. Please correct me if I am wrong.
    – user37473
    Aug 18, 2015 at 0:05
  • Hi @user37473 , I've made an update to include some Mac specific information. Aug 18, 2015 at 20:16

In Python (3.6) this works for me

import os

os.system(f'''/usr/bin/osascript -e 'tell app "Microsoft Excel" to close (every window whose name is "title-of-window")' ''')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .