python实现doc转docx,以及docx转html

python里面实现doc转html貌似有点麻烦,这里先把doc转为docx,然后再转为html,代码如下

import win32com.client as wc   # doc转docx用
from pydocx import PyDocX      # docx转html用

'''
doc文件转docx文件
fullpath:路径+文件名(不带后缀)
如:D:\\test\\文件1
'''
def doc2docx(fullpath):
    word = wc.Dispatch("WORD.Application")   # 启动word进程
    word.displayalerts=0  # 不警告
    word.visible=0        # 不显示
    #print(fullpath)
    doc = word.Documents.Open(fullpath + '.doc')  # D:\\test\\文件名1.doc
    doc.SaveAs(fullpath,12, False, "", True, "", False, False, False, False)  #转为docx
    doc.Close()
    word.Quit()

'''
docx转html
fullpath:路径+文件名(不带后缀)
如:D:\\test\\文件2
'''
def docx2html(fullpath):
    html = PyDocX.to_html(fullpath + ".docx")  # 转为html,如:D:\\test\\文件2.docx
    f = open(fullpath + ".html", 'w', encoding="utf-8")  # 变为如:D:\\test\\文件2.html
    f.write(html)
    f.close()

 

你可能感兴趣的:(python)