公司归档文件比较麻烦,于是用Python写了个程序自动归档,运行无错误。但是运行完后问题就来了,自己手动打开word文档时速度变得奇慢,打开一个文档需要1~2min,请各位同仁帮我看看。下为源代码
#归档.py
import os
import re
from win32com import client as wc
def file(paths,sname,version):
for path1 in os.listdir(paths):
m=re.match(r'.*\.docx?$',path1)
if m:
path0=paths+'\\'+path1
print(path0)
word=wc.Dispatch('Word.Application')
w=word.Documents.Open(path0)
s=str(word.ActiveDocument.Sections[3].Headers[0])
d=str(word.ActiveDocument.Sections[3].Footers[0])
t1=re.match(r'TE.{14}',d)
T1=t1.group(0)
t2=re.search(r'(\w.*\w)',s)
T2=t2.group(0)
if T2.find('\r')>=0:
T2=T2.replace('\r','')
t3=re.search(r'(共\d+页)',d)
T3=t3.group(0)
w.Close()
word.Quit()
T=[T1,T2,sname,version,T3]
M='+'.join(T)
path3=paths+'\\'+M+'.doc'
print(path3)
os.renames(path0,path3)
print('done')
paths=input('Please enter a path:')
sname=input('Please enter a name for short:')
version=input('Please enter version(default A):')
if version=='':
version='A'
file(paths,sname,version)
还有就是程序在DOS窗口运行时经常会卡住,不能往下执行。使用IDLE编辑器则运行良好。
跪谢!
你把 .docx 改成了 .doc
看看你的文档是不是原来打开就很慢~
太多的格式,图片,外链……
word对象移到for外边~