Python读取当前目录下的所有文件及写入到word中

废话不多说,直接贴脚本
需要安装 docx 插件!!!需要安装 docx 插件!!!需要安装 docx 插件!!!

# -*- coding: utf-8 -*-
import os
import docx

def listdir(path, list_name):  #传入存储的list
  for file in os.listdir(path):
      file_path = os.path.join(path, file)
      if os.path.isdir(file_path):
          listdir(file_path, list_name)
      else:
        if "static" not in file_path and ".ico" not in file_path and ".iml" not in file_path and ".git" not in file_path and "mvnw" not in file_path and "i18n" not in file_path and "OpenDevice." not in file_path and "images" not in file_path and ".gitignore" not in file_path and ".mvn" not in file_path and "exceltemplates" not in file_path and ".xls" not in file_path and "META-INF" not in file_path:
            list_name.append(file_path)

list_name = list()
listdir("D:\workspaces\wms-bed", list_name)
print(list_name)

document =  docx.Document()
for fileName in list_name:
    head = fileName.replace("D:\workspaces\wms-bed\\", "")
    document.add_heading(head, 3) #直接添加标题
    print(fileName)

    with open(fileName, encoding='utf-8') as file_obj:
        content = file_obj.read()
        document.add_paragraph(content)

document.save("D:/test1.docx")  # 保存文档
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值