1

I am trying to copy data from one excel file to another automatically using python, currently i have to manually update the date in the excel file name every morning. Is there a way to automatically update the date in the excel file name. I am very new to any form of programming, trying to learn to keep my job.

I have tried to use the time date function and declare this as a variable and copy this into code but no luck

import datetime

Filedate= (datetime.date.today()-datetime.timedelta(1))

exceldate= Filedate.strftime("%Y",)+Filedate.strftime("%m",)+Filedate.strftime("%d",)


import pyexcel as p

p.save_book_as(file_name="Q:\Valuations\Currency Options\YieldX Daily Statsexceldate.xls",#CHANGE DATE #manual entry. 
               dest_file_name='YieldX Daily Stats20190522.xlsx')#CHANGE DATE manual entry
3
  • You need to update only one file name or multiple? The program should by working in background? Commented May 31, 2019 at 6:09
  • Multiple. Yes it does work in the background I just click run using Jupyter and it runs files I only pasted the part of the code I am struggling with. Many thanks for the quick response Commented May 31, 2019 at 6:14
  • what is the date format in files? Commented May 31, 2019 at 6:34

2 Answers 2

3

My approach is to split filename into part that contains date and the rest, then replace date with current one.

import os
import datetime
import re

# get xls files
xls_files = [file for file in os.listdir(os.getcwd()) if file.endswith('.xls')]

# get current date
now = datetime.datetime.now()

# change names
for item in xls_files:
    # split name and date part
    name_parts = item.split('.')
    get_date = re.findall('\d+-\d+-\d+', name_parts[0])
    name_string_part = name_parts[0].replace(get_date[0], '')
    # create new name
    new_name = name_string_part + str(now.day) + '-' + str(now.month) + '-' + str(now.year) + '_' + '.xls'
    # rename file
    os.rename(item, new_name)
Sign up to request clarification or add additional context in comments.

1 Comment

Your code is also good may use it in another way. Thanks for the response I have being struggling for weeks with the issue
0

I believe what you are trying to do is, open a excel file everyday, and rename its filename to current date, where the previous excel file will have the date of yesterday.

import datetime
import pyexcel as p

yesterday = (datetime.date.today()-datetime.timedelta(1)).strftime("%Y%m%d")

today = datetime.date.today().strftime("%Y%m%d")

p.save_book_as(file_name="Q:\Valuations\Currency Options\YieldX Daily Stats" + yesterday + ".xls",
               dest_file_name='YieldX Daily Stats' + today + '.xlsx')

The above code, when executed will change the name of the .xls file which was created yesterday (with its Timestamp), to the current date.

Example:-

If a file named YieldX Daily Stats20190530.xls existed yesterday, today its name will be modified to YieldX Daily Stats20190531.xls

1 Comment

Thank you thank you, this code is applicable on some many other areas,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.