本篇内容介绍了“如何用python实现自动化办公邮件合并功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
代码如下:
import pandas as pd import numpy as np import random import csv from mailmerge import MailMerge #生成姓名 def create_fname_data(): word_select = [] for i in range(97, 123): word_select.append(chr(i)) print(word_select) name_select = [] for i in range(6): name = '' for i in range(5): name += random.choice(word_select) name+=' ' for i in range(5): name += random.choice(word_select) name_select.append(name.title()) print(name_select) return name_select #生成邮箱 def create_mail(): word_select=[str(i) for i in range(9)] all_number_select=[] for i in range(6): number_select='' for i in range(10): number_select+=random.choice(word_select) endwith=['@163.com','@qq.com','@icloud.com'] number_select+=random.choice(endwith) all_number_select.append(number_select) return all_number_select #创建csv文件 def add_csv(): df=pd.DataFrame( { 'name':pd.Series(create_fname_data()), 'date':pd.Series(pd.date_range('20210101',periods=6)), "mail":pd.Series(create_mail()), } ) print(df) df.to_csv('./data/random_data.csv') if __name__ == '__main__': add_csv() with open('./data/random_data.csv',encoding='utf-8') as f:#邮件合并数据目录位置 reader=csv.reader(f) next(reader) for index,name,date,mail in reader: doc=MailMerge('./data/test1.docx')#模板文件目录 print(name,mail,date) doc.merge(name=name,mail=mail,date=date)#邮件合并 doc.write(f'./data/{name}.docx')#写入文件
下图是自动生成的word文档。