批量将word数据写入txt文件

# In[1]
import os
from docx import Document


dir_lists = os.listdir()

for dir in dir_lists:
    if os.path.isdir(dir):
        # print(dir)
        words_lst = os.listdir(dir)
        for word in words_lst:
            url_path = os.getcwd()
            url_path = url_path + '\\' + dir + '\\'
            url_path = url_path + word
            print(url_path[:-5])
            if word.find('txt') != -1:
                continue
            f = open(url_path[:-5] + '.txt', 'w', encoding='utf-8')
            file = Document(url_path)
            print('段落数:'+str(len(file.paragraphs)))
            for para in file.paragraphs:
                f.write(para.text)
            f.close()

posted @ 2021-09-09 22:18  douzujun  阅读(92)  评论(0编辑  收藏  举报