简体   繁体   English

尝试创建一个脚本来交叉检查 excel 文件是否已更新,如果已更新,则发送 email

[英]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.基本上我经营一家商店,我们每天更新一次获取外部数据的 excel 文件,然后我们通过 email 将更新的文件发送给一群人。 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.外部数据每天出现一次,有时在凌晨 1 点出现,有时在凌晨,有时更晚 - 但主要是在夜间/清晨出现,所以当我开始工作时,应该更新外部数据集。

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.让 excel 文件加载并通过 email 发送似乎很简单,但如果假设外部数据集尚未更新,我不希望发送 email。

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基于此链接生成文件的 MD5 校验和

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建 python 脚本,以便当目录中的 csv 文件在过去 24 小时内未更新时发送 email? - How do I create a python script such that it sends an email when csv files in a directory has not updated in the last 24 hours? 为什么即使在 SageMaker 中成功执行了生命周期脚本,package 也没有更新? - Why package is not updated even the lifecycle script has been executed successfully in SageMaker? 有没有办法显示工作表已通过API进行了远程更新? - Is there a way to show the sheet has been updated remotely via the API? 如何检查TensorFlow中重量张量的更新次数? - How to check number of times a Weight Tensor has been updated in TensorFlow? 访问字段已更新的时间(Django模型) - Accessing the time where a field has been updated (Django models) Python访问具有Defualt值已更新的值的字典 - Python Accessing Dict That Has Defualt Values That Have Been Updated 如何检查 RSS 提要是否已在 Python 中更新? - How to check if an RSS feed has been updated in Python? Python 说“TrackerMedianFlow_create()”不再是 cv2 的属性,库更新了吗? - Python says that "TrackerMedianFlow_create()" is no longer an attribute of cv2, Has the Library been updated? 不支持Etag时如何检查Rss是否已更新 - How to check if Rss has been updated when there's no Etag support Django:如何判断.save()之后是否已更新对象 - Django: How to tell if an object has been updated after .save()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM