简体   繁体   中英

Trying to create a script that crosschecks if excel file has been updated and if it has been updated, sends out an email

basically i run a shop where we once a day update an excel file that gets external data and then we send the updated files via email to a group of people. We do this with quite a lot of reports, so i want to write a script that does this automatically.

The external data comes once a day and sometimes it comes at 1 in the morning, sometimes early in the morning, sometimes later - but mainly it comes during the night/early morning, so when i get to work the external dataset should be updated.

My question is:

Getting the excel file loaded and send via email seems pretty straight forward but i dont want the email to be send if lets say the external data set has not been updated.

How do i compare the day before dataset with todays dataset, without saving yesterdays dataset as a seperate file on my computer, as this would build up to quite alot of files.

Based on this link Generating an MD5 checksum of a file

Code could look something like this:

import hashlib
from os.path import exists


def get_oldsum():
    global last_checksum
    if exists("lastsum"):
        with open("lastsum","r") as file:
            last_checksum = file.readline()
    else:
        last_checksum = ""


def save_newsum(newsum):
    with open("lastsum", "w") as file:
        file.write(newsum)


def make_new_hash(file):
    with open(file,"rb") as file:
        file_hash = hashlib.md5()
        while chunk := file.read(8192):
            file_hash.update(chunk)
    return file_hash.hexdigest()


if __name__ == "__main__":
    get_oldsum()
    new_hash = make_new_hash("shared_reports.xlsx")
    print("This MD5 Hash:",new_hash)
    if new_hash != last_checksum:
        print("New Hash! We should trigger our mail-function asap...")
    save_newsum(new_hash)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM