Python自動(dòng)化辦公之Excel拆分并自動(dòng)發(fā)郵件
需求
需要向大約 500 名用戶發(fā)送帶有 Excel 附件的電子郵件,同時(shí)必須按用戶從主 Excel 文件中拆分?jǐn)?shù)據(jù)以創(chuàng)建他們自己的特定文件,然后將該文件通過電子郵件發(fā)送給正確的用戶
需求解析
大致的流程就是上圖,先拆分 Excel 數(shù)據(jù),提取出對(duì)應(yīng)的郵件地址和用戶的數(shù)據(jù)信息,再自動(dòng)添加到郵件的附件當(dāng)中
代碼實(shí)現(xiàn)
首先我們先來看下我們手中 Excel 的數(shù)據(jù)形式是怎么樣的。
import datetime
import os
import shutil
from pathlib import Path
import pandas as pd
src_file = Path.cwd() / 'data' / 'Example4.xlsx'
df = pd.read_excel(src_file)
df.head()
可以看出,CUSTOMER_ID 就是那個(gè)唯一的用戶 ID,下面我們以該字段來分組,得到如下數(shù)據(jù)。
customer_group = df.groupby('CUSTOMER_ID')
for ID, group_df in customer_group:
print(ID)
>>>Output>>>
A1000
A1001
A1002
A1005
...
我們?cè)賮砜聪掠脩?A1005 所對(duì)應(yīng)的數(shù)據(jù)形式。
接下來我們就為每一個(gè)用戶創(chuàng)建一個(gè) Excel,后面就可以作為附件使用。
attachment_path = Path.cwd() / 'data' / 'attachments'
today_string = datetime.datetime.today().strftime('%m%d%Y_%I%p')
attachments = []
for ID, group_df in customer_group:
attachment = attachment_path / f'{ID}_{today_string}.xlsx'
group_df.to_excel(attachment, index=False)
attachments.append((ID, str(attachment)))
我們來看下變量 attachments 所包含的數(shù)據(jù)吧。
[('A1000',
'c:\\Users\\luobo\\notebooks\\2020-10\\data\\attachments\\A1000_01162021_12PM.xlsx'),
('A1001',
'c:\\Users\\luobo\\notebooks\\2020-10\\data\\attachments\\A1001_01162021_12PM.xlsx'),
('A1002',
'c:\\Users\\luobo\\notebooks\\2020-10\\data\\attachments\\A1002_01162021_12PM.xlsx'),
('A1005',
'c:\\Users\\luobo\\notebooks\\2020-10\\data\\attachments\\A1005_01162021_12PM.xlsx')]
最后我們可以通過將 DataFrame 合并在一起來生成帶有電子郵件地址的文件列表。
email_merge = pd.merge(df, df2, how='left')
combined = email_merge[['CUSTOMER_ID', 'EMAIL', 'FILE']].drop_duplicates()
得到的 DataFrame 如下:
我們已經(jīng)收集了客戶名單、他們的電子郵件和附件,現(xiàn)在我們就可以用 Outlook 發(fā)送一封電子郵件了。
import win32com.client as win32
today_string2 = datetime.datetime.today().strftime('%b %d, %Y')
class EmailsSender:
def __init__(self):
self.outlook = win32.Dispatch('outlook.application')
def send_email(self, to_email_address, attachment_path):
mail = self.outlook.CreateItem(0)
mail.To = to_email_address
mail.Subject = today_string2 + ' Report'
mail.Body = """Please find today's report attached."""
mail.Attachments.Add(Source=attachment_path)
# Use this to show the email
#mail.Display(True)
# Uncomment to send
#mail.Send()
通過上面這個(gè)簡(jiǎn)單的類,我們可以生成電子郵件并附加 Excel 文件。
同時(shí)我們還注意到,這里使用了 win32,關(guān)于這個(gè)庫的具體使用,我們?cè)谙麓蔚奈恼轮性倬唧w說明吧。
email_sender = EmailsSender()
for index, row in combined.iterrows():
email_sender.send_email(row['EMAIL'], row['FILE'])
最后,我們?cè)侔阉猩傻?Excel 存檔,以備后面審查、比對(duì)等。
archive_dir = Path.cwd() / 'archive'
for f in attachments:
shutil.move(f[1], archive_dir)
至此,我們的編碼結(jié)束,整體來看還是比較簡(jiǎn)單的。