[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