Skip to content

Instantly share code, notes, and snippets.

@garymanley
Created December 28, 2017 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garymanley/d6ff66218747d4463af921588226a722 to your computer and use it in GitHub Desktop.
Save garymanley/d6ff66218747d4463af921588226a722 to your computer and use it in GitHub Desktop.
Open and Close Xlwings
import xlwings as xw
import glob
import os
import datetime
### Not needed for this script but is used in many others
import win32com.client
### set whether to run this section
Excel = True
### run this section
if Excel:
### Get list of files matching the basic file name
list_of_files = glob.glob('File Location\\Filename*.xlsx')
### get the latest file
latest_file = max(list_of_files, key=os.path.getctime)
### Set XLwings app to visible - I had a problem with this so just to be sure
xw.App(visible=True)
### Open the latest file
wb = xw.Book(latest_file)
### Select the Sheet, Sheet Name
wb.sheets['Sheet Name'].select()
### Set Filename to filename with todays date
filename = 'Folder Location\\Filename '+datetime.datetime.today().strftime('%d%m%y')+'.xlsx'
wb.save(filename)
wb.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment