Skip to content

Commit 2ce6a8b

Browse files
committed
add code
1 parent 56c32c0 commit 2ce6a8b

File tree

1 file changed

+70
-0
lines changed
  • doudou/python-office-automation

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import docx
2+
from docx2pdf import convert
3+
import openpyxl
4+
import smtplib
5+
from email.mime.text import MIMEText
6+
from email.mime.multipart import MIMEMultipart
7+
from email.mime.application import MIMEApplication
8+
9+
10+
# 生成对应的邀请函,并转存pdf格式
11+
def get_invitation(name):
12+
doc = docx.Document("template.docx")
13+
for para in doc.paragraphs:
14+
if '<name>' in para.text:
15+
for run in para.runs:
16+
if '<name>' in run.text:
17+
run.text = run.text.replace('<name>', name)
18+
doc.save(f'./邀请函/{name}.docx')
19+
convert(f"./邀请函/{name}.docx")
20+
21+
22+
smtp = smtplib.SMTP(host="smtp.qq.com", port=587)
23+
smtp.login('235977@qq.com', "ruybefkipoo")
24+
25+
26+
def send_email(name, email):
27+
msg = MIMEMultipart()
28+
msg["subject"] = f"您好,{name},您的邀请函!"
29+
msg["from"] = "2352180977@qq.com"
30+
msg["to"] = email
31+
32+
html_content = f"""
33+
<html>
34+
<body>
35+
<p>您好:{name}<br>
36+
<b>欢迎加入Python进阶者学习交流群,请在附件中查收您的门票~</b><br>
37+
点击这里了解更多:<a href="https://www.pdcfighting.com">演唱会主页</a>
38+
</p>
39+
</body>
40+
</html>
41+
"""
42+
html_part = MIMEText(html_content, "html")
43+
msg.attach(html_part)
44+
with open(f"./邀请函/{name}.pdf", "rb") as f:
45+
doc_part = MIMEApplication(f.read())
46+
doc_part.add_header("Content-Disposition", "attachment", filename=name)
47+
# 把附件添加到邮件中
48+
msg.attach(doc_part)
49+
# 发送前面准备好的邮件
50+
smtp.send_message(msg)
51+
# 如果放到外边登录,这里就不用退出服务器连接,所以注释掉了
52+
# smtp.quit()
53+
54+
55+
def get_username_email():
56+
workbook = openpyxl.load_workbook("names.xlsx")
57+
worksheet = workbook.active
58+
for index, row in enumerate(worksheet.rows):
59+
if index > 0:
60+
name = row[0].value
61+
email = row[3].value
62+
# print(name, email)
63+
# print(f"{name}邀请函正在生成...")
64+
# get_invitation(name)
65+
send_email(name, email)
66+
67+
68+
if __name__ == '__main__':
69+
get_username_email()
70+
# get_invitation('Python进阶者')

0 commit comments

Comments
 (0)