Search code examples
pythonconcatenation

Opening xlxs workbook in Python, automating file name


I'm a heavy R user but very new to Python. I'm trying to edit a Python script that is part of a larger workflow. The script starts off by opening an .xlsx document. This document is produced in an earlier part of the workflow and the naming convention is always YYYYMMDD_location (ex: 20191101_Potomac). I would like to set up the Python script so that it automatically pastes today's date and that location variable into the path.

In R, to not have to manually update the path name each time I run the script, I would do something like:

#R

library(openxlsx)

dir_name <- 'C:/path/to/file/'
location_selection <- 'Potomac'
date <- (paste(format(Sys.Date(),"%Y%m%d"))

open.xlsx(paste0(dir_name, date, "_", location_selection, ".xlsx")

I've looked up how to set up something similar in Python (ex: Build the full path filename in Python), but not making much progress in producing something that works.

# Python

import datetime

dir_name = 'C:\path\to\file'
location_selection = todays_date.strftime('%Y%m%d')+'_Potomac'
suffix = '.xlsx'
file = os.path.join(dir_name, location_selection + suffix)

book = xlrd.open_workbook(file)



Solution

  • No need to use os module as you provide the full directory path.

    from datetime import date
    
    dir_name = r'C:\path\to\file'
    location_selection = f"{date.today().strftime('%Y%m%d')}_Potomac"
    suffix = '.xlsx'
    
    file_name = f'{dir_name}\{location_selection}{suffix}'
    
    book = xlrd.open_workbook(file_name)
    

    We can add more variables as well :

    from datetime import date
    
    dir_name = r'C:\path\to\file'
    today = date.today().strftime('%Y%m%d')
    location = "Potomac"
    suffix = '.xlsx'
    
    file_name = f'{dir_name}\{today}_{location}{suffix}'
    
    book = xlrd.open_workbook(file_name)
    

    Finaly we can create a function that could be reused :

    from datetime import date
    
    def get_filename(location, dir_name=r'C:\path\to\file', suffix=".xlsx", date_format = '%Y%m%d'):
        today = date.today().strftime(date_format)
        return f'{dir_name}\{today}_{location}{suffix}'
    
    book = xlrd.open_workbook(get_filename("Potomac"))
    book2 = xlrd.open_workbook(get_filename("Montreal"))
    book3 = xlrd.open_workbook(get_filename("NewYork"))