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 文件夹。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值