1 import os,shutil,docx,re,time 2 from win32com import client as wc 3 #从所有级联目录读取文件到指定目录内 4 def count_files(file_dir): 5 count=0 6 for p,d,f in os.walk(file_dir): 7 for c in f: 8 if c.split('.')[-1]=="doc": 9 count +=1 10 src_dir = os.path.join(p, c) 11 print(src_dir) 12 dst_dir = file_dir + "back" 13 if not os.path.exists(dst_dir): 14 os.makedirs(dst_dir) 15 shutil.copy(src_dir, dst_dir) 16 return count 17 #提取每个docx简历文档里面的邮箱地址,我们这里使用python-docx模块来解决pip install python-docx 18 def count_mail(file_dir,dst_file): 19 mail_list = [] 20 for parent,dirctiory,files in os.walk(file_dir): 21 for f in files: 22 doc = docx.Document(os.path.join(parent,f)) 23 pattern = re.compile(r'''([a-zA-Z0-9._%+-]+@[a-zA-Z0-9\t\s.-]+(\.[a-zA-Z0-9\t\s]{2,4}))''', re.VERBOSE) 24 for para in doc.paragraphs: 25 for groups in pattern.findall(para.text): 26 mail_list.append(groups[0].replace(" ","")+";") 27 with open(dst_file,'w')as f: 28 f.writelines(mail_list) 29 print("=====================邮件信息写入成功===================") 30 #由于python-docx模块只能处理docx后缀,我们需要处理doc后缀的文件,必须通过win32com模块来把doc后缀转换成docx 31 def docxTodoc(old_doc,new_doc): 32 word = wc.Dispatch('Word.Application') 33 for parent,directory,files in os.walk(old_doc): 34 for f in files: 35 doc = word.Documents.Open(os.path.join(parent,f)) # 目标路径下的文件 36 new_filepath=os.path.join(new_doc,f.split(".")[0]+".docx") 37 print(new_filepath) 38 doc.SaveAs(new_filepath, 12, False, "", True, "", False, False, False,False) # 转化后路径下的文件 39 doc.Close() 40 print(time.time()) 41 word.Quit() 42 43 44 45 if __name__ == '__main__': 46 print(count_files(r"C:\Users\icestick\Desktop\51job_导出简历_20180917")) 47 count_mail(r"C:\Users\icestick\Desktop\new_doc",r"C:\Users\icestick\Desktop\test.txt" ) 48 old_doc = r"C:\Users\icestick\Desktop\51job_导出简历_20180917" #需要把doc目录转成docx格式的原目录 49 new_doc = r"C:\Users\icestick\Desktop\new_doc" #需要把doc目录转成docx格式的目标目录 50 mail_extract = r"C:\Users\icestick\Desktop\test.txt" #邮箱提取好的文件 51 if not os.path.exists(new_doc): 52 os.mkdir(new_doc) 53 print("=====================目录创建成功======================") 54 docxTodoc(old_doc, new_doc) 55 print("=====================docx格式转换成功===================") 56 count_mail(new_doc, mail_extract) 57 58 else: 59 docxTodoc(old_doc, new_doc) 60 print("=====================docx格式转换成功===================") 61 count_mail(new_doc, mail_extract)