[英]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.