python word 转html,怎么用python将Word转成html?
#coding=utf-8
#文件名:
#BatchConverWords2Html.py
#说明:
#批量将一个文件夹下的所有.doc/.docx文件转为.html文件,需要安装对应的Win32模块
#调用方式:进入源程序目录,命令:pythonBatchConverWords2Html.pyRootDir
fromWin32comimportclientaSWc
importos
Word=wc.Dispatch('Word.Application')
defWordsToHtml(dir):
Forpath,SUbdirs,filesinos.walk(dir):
ForWordFileinfiles:
WordFullName=os.path.join(path,WordFile)
#PRint"Word:"+WordFullName
doc=Word.Documents.Open(WordFullName)
WordFile2=unicode(WordFile,"gbk")
dotIndex=WordFile2.rfind(".")
if(dotIndex==-1):
PRint"********************ERROR:未取得后缀名!"
fileSuffix=WordFile2[(dotIndex+1):]
if(fileSuffix=="doc"orfileSuffix=="docx"):
fileName=WordFile2[:dotIndex]
htmlName=fileName+".html"
htmlFullName=os.path.join(unicode(path,"gbk"),htmlName)
#htmlFullName=unicode(path,"gbk")+"\"+htmlName
PRint"generatehtml:"+htmlFullName
doc.SaveAs(htmlFullName,10)
doc.Close()
Word.QUIt()
PRint""
PRint"Finished!"
if__name__=='__mAIn__':
importsys
iflen(sys.argv)!=2:
PRint"Usage:pythonfuncName.pyrootdir"
sys.exit(100)
WordsToHtml(sys.argv[1])运行结果就是在rootdir目录下的所有Word文档转为简洁版的html网页文件,生成的文件存在原Word同目录下,生成 xxx.files 文件夹。