python 解析docx文档将文档名修改为docx里的合同编号

[root@localhost mnt]# cat rename.py 
#!/usr/bin/python
#coding=utf-8
import os
from docx import Document
path = "/mnt/"
dir=os.listdir(path.decode("utf-8"))
for i in dir:
    if os.path.splitext(i)[1] == ".docx":
        print i
        doc = Document(i)
        for para in doc.paragraphs:
                        #print(para.text)
            #print(para.text.find("合同编号".decode("utf-8")))
                if para.text.find("合同编号".decode("utf-8")) >= 0:
                #print(para.text)
                num = ''.join([x for x in para.text if x.isdigit()])
                #print(num)
                #print(path+i)
                os.rename(path+i,path+num+".docx")                
                break

  

posted @ 2020-07-14 17:36  阿西吧li  阅读(522)  评论(0编辑  收藏  举报