遍历文件夹批量提取word文档

需求场景:

老板让汇总全国100多个分公司的年度述职报告(word),每个分公司传了一个文件夹过来,里面包含分公司各个人员的报告。我现在想批量把这些报告全部都汇总到一个文件夹里,免得一个文件夹一个文件夹找。

实现代码:

import os
import shutil
from pathlib import Path


def collect_word_documents(source_dirs, target_dir):
    """
    从多个源文件夹收集Word文档到目标文件夹

    参数:
        source_dirs: 源文件夹路径列表
        target_dir: 目标文件夹路径
    """
    # 确保目标文件夹存在
    target_path = Path(target_dir)
    target_path.mkdir(parents=True, exist_ok=True)

    # 支持的Word文档扩展名
    word_extensions = {'.docx', '.doc'}

    # 计数器
    copied_count = 0
    skipped_count = 0

    print("开始收集Word文档...")

    for source_dir in source_dirs:
        source_path = Path(source_dir)

        if not source_path.exists():
            print(f"警告: 源文件夹不存在: {source_dir}")
            continue

        print(f"正在处理: {source_dir}")

        # 递归遍历所有文件和子文件夹
        for file_path in source_path.rglob('*'):
            if file_path.is_file() and file_path.suffix.lower() in word_extensions:
                # 生成目标文件路径
                target_file = target_path / file_path.name

                # 处理文件名冲突
                counter = 1
                while target_file.exists():
                    stem = file_path.stem
                    suffix = file_path.suffix
                    target_file = target_path / f"{stem}_{counter}{suffix}"
                    counter += 1

                # 复制文件
                try:
                    shutil.copy2(file_path, target_file)
                    print(f"已复制: {file_path} -> {target_file}")
                    copied_count += 1
                except Exception as e:
                    print(f"错误: 无法复制文件 {file_path}: {e}")
                    skipped_count += 1

    print(f"\n操作完成! 成功复制: {copied_count} 个文件, 跳过: {skipped_count} 个文件")
    print(f"文件已保存到: {target_path.absolute()}")


if __name__ == "__main__":
    # 设置源文件夹列表(可以修改为您的实际文件夹路径),多个文件夹用逗号隔开
    source_directories = [
        r"C:\Users\2025\Desktop\年度述职报告"  
    ]

    # 设置目标文件夹(可以修改为您的目标路径)
    target_directory = r"C:\Users\2025\Desktop\年度述职报告汇总" 

    # 执行收集操作
    collect_word_documents(source_directories, target_directory)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值