同一目录下有多个docx文件
import docx import os list_w = [] for filename in os.listdir(r"F:\xxxx\PycharmProjects\Python学习项目\docx文件"): if filename.endswith('.docx'):#如果文件以.docx结尾 document = docx.Document("F:\\xxxx\\PycharmProjects\\Python学习项目\\docx文件\\"+filename) all_paragraphs = document.paragraphs for paragraph in all_paragraphs: # 打印每一个段落的文字 #print(paragraph.text) list_w.append(paragraph.text) #print(list_w) with open("data.txt","w",encoding="utf-8") as f: for line in list_w: f.write(line+"\n")
写入完成后:
标签:文件,docx,Python,list,paragraphs,txt From: https://www.cnblogs.com/shan-gui-yao/p/18548088