用AI写预约网站(将预约信息自动保存到Excle表格)

#必须在当前文件夹下新建一个templates文件夹,把所有html文件放到这个文件夹下面

from flask import Flask, render_template, request, redirect, url_for

import openpyxl

from datetime import datetime

from pathlib import Path

app = Flask(__name__)

# Excel 文件路径

excel_file = 'appointments.xlsx'

# 辅助函数:将预约信息写入 Excel 文件

def write_to_excel(name, email, date, time):

    # 如果文件不存在,则创建一个新的 Excel 文件

    if not Path(excel_file).exists():

        wb = openpyxl.Workbook()

        sheet = wb.active

        sheet.title = 'Appointments'

        # 添加表头

        sheet.append(['Name', 'Email', 'Date', 'Time', 'Timestamp'])

    else:

        wb = openpyxl.load_workbook(excel_file)

        sheet = wb.active


    # 计算下一个空行的行号

    next_row = sheet.max_row + 1


    # 写入预约信息

    sheet.cell(row=next_row, column=1, value=name)

    sheet.cell(row=next_row, column=2, value=email)

    sheet.cell(row=next_row, column=3, value=date)

    sheet.cell(row=next_row, column=4, value=time)

    sheet.cell(row=next_row, column=5, value=datetime.now().isoformat())


    # 保存 Excel 文件

    wb.save(excel_file)

# 用于存储预约信息的列表(模拟数据库)

appointments = []

@app.route('/')

def index():

    return render_template('index.html', appointments=appointments)

@app.route('/book', methods=['GET', 'POST'])

def book():

    if request.method == 'POST':

        name = request.form['name']

        email = request.form['email']

        date = request.form['date']

        time = request.form['time']


        # 将预约信息添加到 appointments 列表中

        appointments.append({'name': name, 'email': email, 'date': date, 'time': time})


        # 将预约信息写入 Excel 文件

        write_to_excel(name, email, date, time)


        # 重定向到预约成功页面

        return redirect(url_for('index'))


    # GET 请求时渲染预约表单页面

    return render_template('book.html')

if __name__ == '__main__':

    app.run(host='0.0.0.0', port=5000, debug=True)



#index.html文件代码

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>预约列表</title>

</head>

<body>

    <h1>预约列表</h1>

    <ul>

        {% for appointment in appointments %}

        <li>

            <strong>姓名:</strong> {{ appointment.name }}<br>

            <strong>邮箱:</strong> {{ appointment.email }}<br>

            <strong>日期:</strong> {{ appointment.date }}<br>

            <strong>时间:</strong> {{ appointment.time }}<br>

        </li>

        {% endfor %}

    </ul>

    <a href="/book">预约</a>

</body>

</html>


#book.html文件代码

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>预约表单</title>

</head>

<body>

    <h1>预约表单</h1>

    <form method="post" action="/book">

        <label for="name">姓名:</label><br>

        <input type="text" id="name" name="name" required><br><br>


        <label for="email">邮箱:</label><br>

        <input type="email" id="email" name="email" required><br><br>


        <label for="date">日期:</label><br>

        <input type="date" id="date" name="date" required><br><br>


        <label for="time">时间:</label><br>

        <input type="time" id="time" name="time" required><br><br>


        <input type="submit" value="提交预约">

    </form>

    <br>

    <a href="/">返回预约列表</a>

</body>

</html>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容