Home > database >  Python - Making copy of live Excel file every hour - no data refreshment
Python - Making copy of live Excel file every hour - no data refreshment

Time:10-19

I am working with an 'Live' Excel file for which the data connection is refreshed every 5 minutes (including time stamp). As previous data is lost with every refreshment, I want to make a copy of the data to a new file every hour, for the purpose of data analysis.

I wrote this code, which in general works, as it creates a new file every hour by copying the data from the live Excel:

import pandas as pd
from datetime import datetime as dt
import schedule
import time

df = pd.read_excel("import.xlsx", sheet_name='Raw Data')
def export():
    now =dt.now()
    dt_string = now.strftime("%Y%m%d %H%M%S")
    df.to_excel("export"   dt_string   ".xlsx", sheet_name='Raw Data')

schedule.every(3600).seconds.do(export)

while 1:
    schedule.run_pending()
    time.sleep(1)

Unfortunately, all the new Excel files have the same data and time stamp (the moment I started running the code). First I thought, besides refreshing the data, it is also required to save the live excel file every x minutes, however, manually saving the file from time to time does not make any difference.

Are there any changes I can make to my Python code to have the latest data in each copy every hour, instead of only the initial data of when the run was started?

CodePudding user response:

I think the reason the data is not changing is because your df variable is set once, outside of the export() function. To fix you should just have to move the assignment into your function:

import pandas as pd
from datetime import datetime as dt
import schedule
import time

def export():
    df = pd.read_excel("import.xlsx", sheet_name='Raw Data') #Moved this line
    now =dt.now()
    dt_string = now.strftime("%Y%m%d %H%M%S")
    df.to_excel("export"   dt_string   ".xlsx", sheet_name='Raw Data')

schedule.every(3600).seconds.do(export)

while 1:
    schedule.run_pending()
    time.sleep(1)
  • Related