温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python通过docx模块解决doc及docx后缀文件内容的处理

发布时间:2020-07-31 03:52:15 来源:网络 阅读:3172 作者:icestick8586 栏目:软件技术
import os,shutil,docx,re,time
from win32com import client as wc
#从所有级联目录读取文件到指定目录内
def count_files(file_dir):
    count=0
    for p,d,f in os.walk(file_dir):
        for c in f:
            if c.split('.')[-1]=="doc":
                count +=1
                src_dir = os.path.join(p, c)
                print(src_dir)
                dst_dir = file_dir + "back"
                if not os.path.exists(dst_dir):
                    os.makedirs(dst_dir)
                shutil.copy(src_dir, dst_dir)
    return count
#提取每个docx简历文档里面的邮箱地址,我们这里使用python-docx模块来解决pip install python-docx
def count_mail(file_dir,dst_file):
    mail_list = []
    for parent,dirctiory,files in os.walk(file_dir):
        for f in files:
            doc = docx.Document(os.path.join(parent,f))
            pattern = re.compile(r'''([a-zA-Z0-9._%+-]+@[a-zA-Z0-9\t\s.-]+(\.[a-zA-Z0-9\t\s]{2,4}))''', re.VERBOSE)
            for para in doc.paragraphs:
                for groups in pattern.findall(para.text):
                    mail_list.append(groups[0].replace(" ","")+";")
    with open(dst_file,'w')as f:
        f.writelines(mail_list)
    print("=====================邮件信息写入成功===================")
#由于python-docx模块只能处理docx后缀,我们需要处理doc后缀的文件,必须通过win32com模块来把doc后缀转换成docx
def docxTodoc(old_doc,new_doc):
    word = wc.Dispatch('Word.Application')
    for parent,directory,files in os.walk(old_doc):
        for f in files:
            doc = word.Documents.Open(os.path.join(parent,f))  # 目标路径下的文件
            new_filepath=os.path.join(new_doc,f.split(".")[0]+".docx")
            print(new_filepath)
            doc.SaveAs(new_filepath, 12, False, "", True, "", False, False, False,False)  # 转化后路径下的文件
            doc.Close()
            print(time.time())
    word.Quit()

if __name__ == '__main__':
    print(count_files(r"C:\Users\icestick\Desktop\51job_导出简历_20180917"))
    count_mail(r"C:\Users\icestick\Desktop\new_doc",r"C:\Users\icestick\Desktop\test.txt" )
    old_doc = r"C:\Users\icestick\Desktop\51job_导出简历_20180917"  #需要把doc目录转成docx格式的原目录
    new_doc = r"C:\Users\icestick\Desktop\new_doc"                  #需要把doc目录转成docx格式的目标目录
    mail_extract = r"C:\Users\icestick\Desktop\test.txt"            #邮箱提取好的文件
    if not os.path.exists(new_doc):
        os.mkdir(new_doc)
        print("=====================目录创建成功======================")
        docxTodoc(old_doc, new_doc)
        print("=====================docx格式转换成功===================")
        count_mail(new_doc, mail_extract)

    else:
        docxTodoc(old_doc, new_doc)
        print("=====================docx格式转换成功===================")
        count_mail(new_doc, mail_extract)
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI