Python 将一个目录下的所有word文档转为txt

需要下载安装win32com,并且注意要和你的python版本和32bit or 64bit一致


#!D:\\My Python\\Trans_to_txt.py
#注意Windows下路径表示 
from win32com import client as wc
import os


print('Enter your Director\'s path:')
mypath = input()
all_FileNum = 0

def Translate(level, path):
    global all_FileNum
    '''
    将一个目录下所有doc文件转成txt
    '''
    #该目录下所有文件的名字
    files = os.listdir(path)
    for f in files:
        if (f[0] == '~' or f[0] == '.'):
            continue
        new = path + '\\\\' + f
        print(new)
        #除去后边的.doc后缀
        tmp = new[:-4]
        #改成txt格式
        word = wc.Dispatch('Word.Application')
        doc = word.Documents.Open(tmp)
        doc.SaveAs(tmp + '.txt', 4)
        doc.Close()
        all_FileNum = all_FileNum + 1
if __name__ == '__main__':
    Translate(1, mypath)
    print('文件总数 = ', all_FileNum)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值