from docx import Document
import os
from win32com import client
file_name="D:/BaiduNetdiskDownload/文案大全匯總"
file_list=os.listdir(file_name)
data_list=[]
for one_file in file_list:
dir_file_name=file_name+"/"+one_file
data_list.append(one_file+"\r")
if "docx" in dir_file_name:
try:
data=Document(dir_file_name)
data_list+=[i.text.strip()+"\r" for i in data.paragraphs]
except:
print()
else:
word = client.Dispatch("Word.Application")
# print("已處理文件:"+files[0])
# 打開文件
try:
doc = word.Documents.Open(dir_file_name)
doc.SaveAs("{}x".format(dir_file_name), 12) # 12表示docx格式
doc.Close()
os.remove(dir_file_name)
except:
print("沒有正常")
with open("25.txt","w",encoding="utf-8") as f:
f.writelines(data_list)