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 文件夾。
總結
以上是生活随笔為你收集整理的python word 转html,怎么用python将Word转成html?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 呀~ 一个.java的源文件可以写这么多
- 下一篇: cmd oracle 连接实例_C#连接