2024 Python-Flask框架:网页版 邮件超时自动提醒器(超简单)

news2025/2/22 3:15:32

首先安装flask框架

pip install flask
pip install pywin32
pip install pandas
pip install datetime

然后根目录下,创建 app.py 和 templates文件夹

(注意我们的原时间是年,周,日的计算方式)

from flask import Flask, render_template, request, redirect, url_for
import pythoncom
import pandas as pd
import win32com.client as win32
from datetime import datetime

app = Flask(__name__)


def get_outlook_email():
    """Get the user's Outlook email address"""
    pythoncom.CoInitializeEx(0)  # Initialize the COM library in the current thread
    outlook = win32.Dispatch('Outlook.Application').GetNamespace('MAPI')
    for account in outlook.Accounts:
        if account.DeliveryStore:
            return account.DeliveryStore.DisplayName
    return None

def send_emails(file_path):
    """Send emails with the selected file as an attachment"""
    if not file_path:
        return

    try:
        pythoncom.CoInitializeEx(0)  # Initialize the COM library in the current thread
        # ... (rest of the send_emails function remains the same)

        # Function to convert the "yearweek.day" format to a datetime object
        def year_week_day_to_date(year_week_day):
            try:
                year = int(str(year_week_day)[:2]) + 2000  # Assuming 2000 as the base year
                week = int(str(year_week_day)[2:4])
                day = int(str(year_week_day)[-1])

                # Check if the week number is valid
                if not (1 <= week <= 53):
                    raise ValueError("Invalid week number")
                date = datetime.strptime(f'{year} {week} {day}', '%Y %W %w')
                return date
            except ValueError as e:
                print(f"Error converting {year_week_day}: {e}")
                return None

        # Get the current date
        current_date = datetime.now()

        # Convert the current date to "year.week.day" format
        current_year = current_date.isocalendar()[0]
        current_week = current_date.isocalendar()[1]
        current_day = current_date.isocalendar()[2]
        current_date_format = f"{current_year}{current_week:02d}{current_day}"

        # Open Outlook and call the API
        outlook = win32.Dispatch('Outlook.Application')

        df['Name'] = df['Name'].astype(str)  # Convert the 'Name' column to string data type

        # Check dates and send emails
        for index, row in df.iterrows():
            prognosetermine = row['过期时间']
            full_name = row['英文名']

            # Check if the 'Name' column is empty
            if pd.isna(full_name) or full_name.strip() == '':
                print(f"Row {index}: Name is empty, skipping...")
                continue

            # Convert the full name to email format
            name_parts = full_name.split()
            email_name = ''
            if len(name_parts) >= 2:
                first_name, last_name = name_parts[0], name_parts[-1]
                email_name = f'{first_name.lower()}.{last_name.lower()}'

            to_email = f'{email_name}@outlook.com'

            prognose_date = year_week_day_to_date(prognosetermine)

            if prognose_date is not None and current_date > prognose_date:
                # Create and send the email
                mail_item = outlook.CreateItem(0)
                mail_item.Subject = "超时提醒"
                mail_item.BodyFormat = 1
                mail_item.Body = f"""
你好 {full_name}, 你已经超时!
"""
                mail_item.To = to_email
                attachment = mail_item.Attachments.Add(file_path)
                mail_item.Send()

    except Exception as e:
        print(f"Error sending emails: {e}")

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        file_path = request.form.get('file_path')
        send_emails(file_path)
        return redirect(url_for('index'))

    email_address = get_outlook_email()
    return render_template('index.html', email_address=email_address)

if __name__ == '__main__':
    app.run(debug=True)

最后在templates文件夹下创建/index.html

<!DOCTYPE html>
<html>
<head>
    <title>Email with Attachment</title>
</head>
<body>
    <h1>Email with Attachment</h1>
    <p>Your Outlook email address: {{ email_address }}</p>
    <form method="post" enctype="multipart/form-data">
        <label for="file_path">Select File:</label>
        <input type="file" id="file_path" name="file_path" required>
        <button type="submit">Send Email</button>
    </form>
</body>
</html>

在vscode中点击执行后:

点击这个url就可以了

打开后有个简单的web页面:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1816859.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

yolov3 详解

文章目录 1、yolov3原理2、损失函数3、yolov3改进4、使用opencv实现yolov35、卷积神经网络工作原理 1、yolov3原理 参考视频 darknet53&#xff1a;52个卷积层和1个全联接层 输入图像为416416 1313 -》 下采样32倍 2626 -》 下采样16倍 5252 -》 下采样8倍 由标注框中心点落在…

离职原因这么回答,off就妥妥的

现在大环境不好&#xff0c;到处都在裁员&#xff0c;好不容易有一次面试机会&#xff0c;结果因为面试回答问题没有回答好&#xff0c;而错失机会&#xff0c;甚至觉得面试表现很好&#xff0c;结果也没拿到offer。 这其中原因除了当前环境下竞争比较激烈外&#xff0c;有些关…

Buffer

Buffer 概念 在Node.js中&#xff0c;Buffer是一个非常重要的内置全局对象&#xff0c;Node.js是基于Chrome V8引擎构建的&#xff0c;V8引擎本身不支持处理二进制数据&#xff0c;因此Node.js引入Buffer来弥补这一不足&#xff0c;Buffer中文译为【缓冲区】&#xff0c;是一…

量产导入 | Tessent Scan 和 ATPG

目标 Upon completion of this module, you should be able to: Use Tessent Scan to insert full scan. Write a scan-inserted netlist file. Write ATPG setup files. lnsert test logic. Create, configure, and balance scan chains. Edit a scan chain order file and …

CentOS停服倒计时不到一个月喊话:“国产操作系统准备好了吗?”安排!

01 CentOS停服倒计时 再过不到20天&#xff0c;CentOS历史将正式终结&#xff01;Redhat公司此前面向全球宣布&#xff0c;将于2024年6月30日停止维护CentOS 7&#xff0c;届时CentOS全系列版本将停止维护&#xff0c;这意味着CentOS时代将彻底终结&#xff0c;而部署在CentOS…

[NCTF 2018]flask真香

打开题目后没有提示框&#xff0c;尝试扫描后也没有什么结果&#xff0c;猜想是ssti。所以尝试寻找ssti的注入点并判断模版。 模版判断方式&#xff1a; 在url地址中输入{7*7} 后发现不能识别执行。 尝试{{7*7}} ,执行成功&#xff0c;继续往下走注入{{7*7}}&#xff0c;如果执…

Linux - 信号阻塞 信号捕捉

Linux - 信号阻塞 & 信号捕捉 信号阻塞信号集操作信号集sigporcmasksigpendingsigaction 信号捕捉用户态与内核态信号捕捉的时机 在博客[Linux - 信号概念 & 信号产生]中&#xff0c;我讲解了信号的基本概念&#xff0c;以及信号是如何产生的&#xff0c;本博客将继续讲…

2024年6月12日 (周三) 叶子游戏新闻

万能嗅探: 实测 网页打开 某视频号、某音、某红薯、某站&#xff0c;可以做到无水印的视频和封面下载功能哦&#xff0c;具体玩法大家自行发挥吧。 WPS免登录一键修改器: 去除烦人的登录且能正常使用 杨奇试戴《黑神话&#xff1a;悟空》豪华版金箍 效果还不错&#xff01;最近…

大型语言模型(LLMs)的后门攻击和防御技术

大型语言模型&#xff08;LLMs&#xff09;通过训练在大量文本语料库上&#xff0c;展示了在多种自然语言处理&#xff08;NLP&#xff09;应用中取得最先进性能的能力。与基础语言模型相比&#xff0c;LLMs在少样本学习和零样本学习场景中取得了显著的性能提升&#xff0c;这得…

西门子PLC位逻辑指令学习(SCL语言)

R_TRIG 参数 功能 当CLK信号出现一个低电平到高电平的跳变时&#xff0c;输出Q导通一个周期。 实例 定义以下类型变量 "R_TRIG_DB"(CLK:"data".source,Q>"data".result); //当source输入出现低电平到高电平跳变&#xff0c;result信号…

File、IO流

File、IO流&#xff08;一&#xff09; 存储数据的方案 以上都是内存中的数据容器&#xff08;记住程序正在处理的数据&#xff0c;能快速运算&#xff09;&#xff0c;它们记住的数据&#xff0c;在断电&#xff0c;或者程序终止时会丢失 程序在内存中处理后的数据想长久的保…

Maven增强插件助你开发快人一步

因为之前的工作中一直用的Maven进行jar包管理&#xff0c;每次新加依赖都要去中央仓库上搜索下坐标&#xff0c;这里为了方便以SerchEveryWhere为入口&#xff0c;增加了一个Maven的搜索tab&#xff0c;输入你想要找的包名即可在idea中直接显示最新版对应的坐标以及cve数&#…

镜舟科技与千丁数科开展战略洽谈,探索智慧空间与数据的深度融合

数据作为当下最重要的生产要素之一&#xff0c;数据分析与智慧空间科技的结合正成为推动企业发展的新引擎 。 5 月 15 日&#xff0c;镜舟科技与千丁数科在北京开展战略合作洽谈。本次会议旨在探索双方在智慧空间科技和数据分析领域的合作潜力&#xff0c;共同推进数字经济的新…

LVS+Keepalived NGINX+Keepalived 高可用群集实战部署

Keepalived及其工作原理 Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案&#xff0c;可以解决静态路由出现的单点故障问题。 VRRP协议&#xff08;虚拟路由冗余协议&#xff09; 是针对路由器的一种备份解决方案由多台路由器组成一个热备组&#xff0c;通过共用的…

WEB基础--TOMCAT服务器

服务器概述 什么是服务器 服务器&#xff1a;就是一个提供为人民服务的机器&#xff0c;这里的服务器主要指计算机服务器&#xff0c;分为两种&#xff1a;服务器软件和硬件服务器&#xff1b; 服务器分类 1、硬件服务器&#xff1a;安装了服务器软件的主机。就相当于高配的…

dijkstra 算法为什么高效?

最短路径算法中&#xff0c;dijkstra(i&#xff0c;j&#xff0c;k 颇有遍历意味) 算法时间效能很好&#xff0c;而 floyd&#xff0c;bellman-ford 算法则优在处理负权重。但这是为什么&#xff1f; 从算法过程看&#xff0c;dijkstra 算法确定了某点最短路径后&#xff0c;它…

单目标应用:基于红嘴蓝鹊优化器RBMO的微电网优化(MATLAB代码)

一、微电网模型介绍 微电网多目标优化调度模型简介_vmgpqv-CSDN博客 参考文献&#xff1a; [1]李兴莘,张靖,何宇,等.基于改进粒子群算法的微电网多目标优化调度[J].电力科学与工程, 2021, 37(3):7 二、红嘴蓝鹊优化器求解微电网 2.1算法简介 红嘴蓝鹊优化器&#xff08;R…

Sharding-JDBC 使用 Nacos 作为配置中心 【下 代码实战】

Sharding-JDBC 使用 Nacos 作为配置中心 【下 代码实战】 1. 实现 ShardingSphereDriverURLProvider 由上一篇博文我们已经知道了 Sharding-JDBC 是基于 Java SPI 机制去加载 并实例化 ShardingSphereDriverURLProvider 的实现类 public interface ShardingSphereDriverURLP…

3个月搞定计算机二级C语言!高效刷题系列进行中

文章目录 前言备考计算机二级C语言为什么考二级C语言&#xff1f;刷题总结后发布系列文章后记免责声明 前言 大家好&#xff0c;我是梁国庆。 计算机二级应该是每一位大学生的必修课&#xff0c;相信很多同学的大学flag中都会有它的身影。 我在大学里也不止一次的想要考计算…

如何下载iSlide软件及详细安装步骤

iSlide插件是一款能够支持PPT和WPS的ppt插件&#xff0c;旨在让每一个人都能够轻松制作出精美的ppt&#xff0c;能够在职场收到更多的关注&#xff0c;而该软件的口号正是「让 PPT 设计简单起来」&#xff0c;该插件是由原来NT插件升级改良过来的&#xff0c;在功能上面进行了全…