python生日祝福HTML
时间: 2024-11-11 15:12:27 浏览: 20
在Python中,你可以创建一个简单的HTML模板来制作生日祝福页面。下面是一个基础的例子:
```python
# 导入必要的模块
from string import Template
# 定义HTML模板字符串
template = Template("""
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Happy Birthday!</title>
</head>
<body>
<h1 style="text-align: center;">Dear [Name],</h1>
<p>Wishing you a very happy birthday filled with joy, love, and lots of memorable moments! May all your dreams come true on this special day.</p>
<img src="[BirthdayImage]" alt="Birthday Cake" style="max-width: 50%; display: block; margin: auto;">
<br>
<p>Sincerely,</p>
<p>Your Name or Website</p>
</body>
</html>
""")
# 替换占位符
name = "张三"
birthday_image_url = "https://example.com/birthday_cake.jpg"
# 执行替换
html_content = template.substitute(Name=name, BirthdayImage=birthday_image_url)
# 将生成的HTML保存到文件
with open("birthday_message.html", "w") as f:
f.write(html_content)
```
在这个例子中,我们使用了Python的`Template`库来处理字符串中的变量替换。将`[Name]`和`[BirthdayImage]`替换成实际的姓名和生日图片URL,就得到了一份个性化的生日祝福邮件。
阅读全文