我有一个问题,要根据文档中的特定文本更改.doc或.docx文件名。
我已经可以使用.txt文件建立此功能。使用以下代码:
import os
import re
pat = "ID number(\\d\\d\\d\\d\\d)" #This is for the text to be found in the file
ext = '.txt' #Type of file the python is searching for
mydir = '' #Path or directory where python is doing its magic
for arch in os.listdir(mydir):
archpath = os.path.join(mydir,arch)
with open(archpath) as f:
txt = f.read()
s = re.search(pat,txt)
if s is None:
continue
name = s.group(1)
newpath = os.path.join(mydir,name)
if not os.path.exists(newpath):
os.rename(archpath,newpath + ext)
有人对此有任何看法吗?