首页 > 编程语言 > 详细

python 将word另存为txt

时间:2017-06-20 00:44:00      阅读:32      评论:0      收藏:0      [点我收藏+]

标签:color   imp   documents   pan   pass   lte   cnblogs   default   word   

 
import os  
import os.path
from win32com import client as wc

c=[]  
 
rootdir=["d:/77"]                #以该路径为实验


def txt(j,c):
        
         word = wc.Dispatch(Word.Application)
                        
         doc = word.Documents.Open(c[j])

         newname=c[j][:-5]+"(translate txt)"

         doc.SaveAs(newname,4)

         doc.Close()

         word.Quit()

         os.remove(c[j])
         
         print("完成")




                                 

def wordt(c):                    #定义函数,进行筛选
        
        for j in range(0,len(c)):
                
                if c[j][-5:] == ".docx":  #寻找docx文件


                        txt(j,c) #

                else:
                        pass
                        
        
        

for i in rootdir:                  #定义函数,查找所有文件
    
        for parent,dirnames,filenames in os.walk(i):  
         
            
            for filename in filenames:    
                 
                c.append(os.path.join(parent,filename))
                
wordt(c)

将docx另存为txt ,并且删除源文件

涉及到office中docx文档的打与另存为命令

相关参考

from win32com import client as wc
word = wc.Dispatch(‘Word.Application‘)
doc = word.Documents.Open(‘c:/test‘)
doc.SaveAs(‘c:/test.text‘, 2)
doc.Close()
word.Quit()


open(r‘c:\text‘,‘r‘)
wdFormatDocument = 0
wdFormatDocument97 = 0
wdFormatDocumentDefault = 16
wdFormatDOSText = 4
wdFormatDOSTextLineBreaks = 5
wdFormatEncodedText = 7
wdFormatFilteredHTML = 10
wdFormatFlatXML = 19
wdFormatFlatXMLMacroEnabled = 20
wdFormatFlatXMLTemplate = 21
wdFormatFlatXMLTemplateMacroEnabled = 22
wdFormatHTML = 8
wdFormatPDF = 17
wdFormatRTF = 6
wdFormatTemplate = 1
wdFormatTemplate97 = 1
wdFormatText = 2
wdFormatTextLineBreaks = 3
wdFormatUnicodeText = 7
wdFormatWebArchive = 9
wdFormatXML = 11
wdFormatXMLDocument = 12
wdFormatXMLDocumentMacroEnabled = 13
wdFormatXMLTemplate = 14
wdFormatXMLTemplateMacroEnabled = 15
wdFormatXPS = 18

over!

python 将word另存为txt

标签:color   imp   documents   pan   pass   lte   cnblogs   default   word   

原文:http://www.cnblogs.com/jjj-fly/p/7051235.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号