使用python将文本转换成doc文档,纯代码 ,一看就懂那种
# -*- coding: utf-8 -*-from docx import Documentimport osimport shutilimport tracebackdef parseFile(file):content = ''file_name = file.split(".")[1]with open(file,encoding='utf...
·
# -*- coding: utf-8 -*-
from docx import Document
import os
import shutil
import traceback
def parseFile(file):
content = ''
file_name = file.split(".")[1]
with open(file,encoding='utf-8') as file_obj:
content = file_obj.read()
print(content)
#创建word
Doc = Document()
Doc.add_heading(file_name)
Doc.add_paragraph(content)
Doc.save(r'D:\temp\yasuo\words\%s.docx' % file_name)
if __name__ == '__main__':
rootdir = r'D:\temp\yasuo'
list = os.listdir(rootdir)
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isfile(path) and path.endswith('txt'):
try:
parseFile(path)
shutil.move(path,r'D:\temp\yasuo\txt')
except :
traceback.print_exc()
print('解析',path,'失败了')
更多推荐
已为社区贡献3条内容
所有评论(0)