文件批量重命名自动化工具:让文件管理更高效
在日常工作中,处理大量文件时,经常会遇到文件命名混乱的问题。例如,签证资料文件夹中可能包含数百个 PDF 文件,文件名如 B180-张三临时身份证.pdf 或 B200-李四-dis.pdf,需要批量重命名为统一格式如 1-张三.pdf 或 2-李四.pdf。手动操作不仅耗时,还容易出错。为了解决这一痛点,我们开发了一款 Python 脚本工具,通过自动化重命名,大幅提升工作效率。
项目背景与功能
该工具针对包含编号和姓名的 PDF 文件,结合 Excel 明细表,自动将原始文件名(如 B180-张三临时身份证.pdf)转换为标准格式(如 1-张三.pdf)。它能:
识别并移除各种后缀(如 临时身份证、dis、-2026 等)。
根据 Excel 表中的编号和姓名映射,生成唯一文件名。
避免文件覆盖,跳过已存在的目标文件名。
通过几分钟的运行,数百个文件即可整齐排列,极大减轻了人工整理的负担。
使用教程
安装准备
安装 Python:
访问 Python 官方网站,下载最新版本(建议 3.9 或以上)。
安装时勾选“Add Python to PATH”,便于命令行使用。
安装完成后,打开命令行,输入 python --version 验证版本。
安装依赖库:
打开命令行,运行以下命令安装 pandas 库:
pip install pandas如果遇到权限问题,可尝试:
pip install pandas --user安装过程中如有错误,更新 pip:
pip install --upgrade pip准备工作环境:
将所有待处理的 PDF 文件和 Excel 明细表(如 签证明细表.xlsx)放入同一文件夹。
确保 Excel 表包含 编号 和 姓名 两列,格式正确。
运行脚本
下载脚本:
从项目来源获取 rename_pdfs.py 文件,保存到上述文件夹。
执行脚本:
打开命令行,导航到文件夹:
cd 你的文件夹路径运行脚本:
python rename_pdfs.py脚本会输出日志,显示重命名或跳过的情况。
检查结果:
运行完成后,检查文件夹,确保文件已按 编号-姓名.pdf 格式重命名。
如import os
import pandas as pd
# This script should be run in the folder containing the PDF files and the Excel file "抵塞签证明细表.xlsx"
# Load the Excel file
df = pd.read_excel('编号姓名明细表.xlsx')
# Create a dictionary mapping name to number
# Assuming columns are '编号' and '姓名'
name_to_num = dict(zip(df['姓名'], df['编号']))
# Define possible suffixes to remove
suffixes = ['临时身份证', '临时', '身份证', '临时居留证', '-2026', '-日期错误', '1', 'dis', '-dis']
# Iterate over all files in the current directory
for file in os.listdir('.'):
if file.lower().endswith('.pdf'):
# Get the base name without extension
base = os.path.splitext(file)[0]
# Split by the first '-', to get prefix and name
if '-' in base:
parts = base.split('-', 1)
if len(parts) == 2:
prefix, name_with_suffix = parts
# Remove defined suffixes and extra spaces
name = name_with_suffix
for suffix in suffixes:
name = name.replace(suffix, '').strip()
# Handle cases where suffix is at the end with or without space
name = name.rstrip('-').rstrip() # Remove trailing '-' and spaces
# Remove any trailing digits if they are not part of the name
while name and name[-1].isdigit():
name = name[:-1].rstrip()
name = name.strip() # Final clean-up
if name in name_to_num:
num = name_to_num[name]
new_name = f"{num}-{name}.pdf"
# Check if the target file already exists
if os.path.exists(new_name):
print(f"Skipped '{file}', target '{new_name}' already exists")
else:
# Rename the file only if the target does not exist
os.rename(file, new_name)
print(f"Renamed '{file}' to '{new_name}'")
else:
print(f"Skipped '{file}', name '{name}' not found in the table")
else:
print(f"Skipped '{file}', invalid format")
else:
print(f"Skipped '{file}', no '-' in name")
注意事项
首次运行前,备份所有文件,避免意外丢失。
如果文件名包含未识别的后缀,可编辑脚本中的 suffixes 列表添加新项。
确保 Excel 表数据完整,姓名与文件一致。
技术实现亮点
该工具采用 Python 编写,利用 pandas 读取 Excel 数据,os 模块处理文件操作。核心逻辑包括:
动态移除多种后缀,确保姓名提取准确。
智能跳过重复文件,保护已有数据。
简洁高效,适用于大规模文件处理。
结语
这款重命名工具不仅适用于签证资料管理,也可扩展到其他场景,如财务报表、档案整理等。希望它能为你的工作带来便利!如有优化需求或使用问题,欢迎留言交流。