将word文件写入txt文件中

import win32com
import win32com.client

def readWordFileToOtherFile(path,toPath):
#调用系统word功能,可以处理doc和docx两种类型的文件
mw = win32com.client.Dispatch(‘Word.Application’)
doc = mw.Documents.Open(path)
#将读出的文件保存到toPath文件中去。2表示的是txt文件。
doc.SaveAs(toPath,2)
#关闭文件
doc.Close()
#退出文件
mw.Quit()

path = r’c:\Users\HP_SSE\Desktop\html.doc’
toPath = r’c:\Users\HP_SSE\Desktop\a.txt’

readWordFileToOtherFile(path,toPath)
(注意代码缩进)

猜你喜欢

转载自blog.csdn.net/weixin_42669661/article/details/81456890