Skip to content

feat(contributor): add batch_rename_by_excel - #183

Closed
my12yyds wants to merge 1 commit into
CoderWanFeng:developfrom
my12yyds:feat/contributor-batch-rename-by-excel
Closed

my12yyds wants to merge 1 commit into
CoderWanFeng:developfrom
my12yyds:feat/contributor-batch-rename-by-excel

Conversation

@my12yyds

Copy link
Copy Markdown
Contributor

说明

按新的贡献规范重做:代码全部放在 contributors/my12yyds/ 下,没有修改任何已有文件(本次提交只有 4 个新增文件,0 个修改)。

文件 作用
contributors/my12yyds/batch_rename_by_excel.py 实现,每个参数和返回值都有注释
contributors/my12yyds/test_batch_rename_by_excel.py 单元测试,8 个用例
skills/file/batch_rename_by_excel/SKILL.md 技能说明
skills/file/batch_rename_by_excel/__init__.py 技能包装

新方法:batch_rename_by_excel

按 Excel 中记录的「原文件名 → 新文件名」映射,批量重命名目录下的文件。

和现有的 replace4filename 互补:replace4filename 是「按规则批量替换文件名中的字符」,本方法适合改名规则不统一、只能逐个列在表格里的场景。

Excel 只需两列:

原文件名 新文件名
a.txt 2026年1月报表.txt
b.txt 2026年2月报表.txt
from skills.file.batch_rename_by_excel import batch_rename_by_excel

result = batch_rename_by_excel(
    excel_path='./rename_list.xlsx',
    folder_path='./files',
    dry_run=True,          # 先预览,确认无误后去掉这个参数
)
print(result['renamed'])

返回 dict,四个键:

  • renamed:已重命名的记录,元素为 (原文件名, 新文件名)
  • skipped:跳过的记录及原因(新旧同名、文件名留空等)
  • missing:Excel 里有、但目录下找不到的文件
  • conflicts:目标文件名已存在,为避免覆盖而跳过

两个安全设计:dry_run=True 可以先预览再执行;目标文件已存在时跳过而不是覆盖。

测试

pytest contributors/my12yyds/test_batch_rename_by_excel.py -q
8 passed

覆盖:正常改名、dry_run 不改文件、源文件缺失、目标已存在不覆盖、空行/同名跳过、自定义列名、缺列报错、Excel 不存在报错。

两个需要你确认的点

  1. skills/file/__init__.py 我没有改。 其他 skill 都在分类的 __init__.py 里用 from .xxx import xxx 注册,但 contributors/ 目录没有 __init__.py,setup.cfg 的 packages = find: 不会把它打进安装包。如果在那里注册,pip install python-office 之后 import skills.file 会因为找不到 contributors 而整体报错。所以技能包装里用了 try/except 兜底:源码仓库中正常导入,安装包环境调用时给出明确提示。如果你希望仍然按老方式注册,我可以改。

  2. 测试文件放在了 contributors/my12yyds/ 里,因为规范写的是「将所有代码放入该文件夹中」。如果希望统一放到 tests/ 下,我可以移动。

关于之前的两个 PR

#181 和 #182 改的是 office/ 下的已有代码,按新规范应该改成 issue。需要的话我关掉这两个 PR,并开对应的 issue 说明问题。

新增方法:按 Excel 中记录的「原文件名 → 新文件名」映射批量重命名文件。

和 replace4filename 互补:后者是按规则替换文件名中的字符,本方法适合改名规则
不统一、只能逐个列在表格里的场景。

按照贡献规范,所有代码都放在 contributors/my12yyds/ 下,未修改任何已有文件:

- contributors/my12yyds/batch_rename_by_excel.py        实现,每个参数和返回值都有注释
- contributors/my12yyds/test_batch_rename_by_excel.py   单元测试,8 个用例
- skills/file/batch_rename_by_excel/SKILL.md            技能说明
- skills/file/batch_rename_by_excel/__init__.py         技能包装

函数返回 dict(renamed / skipped / missing / conflicts),支持 dry_run 先预览,
目标文件已存在时跳过而不覆盖。
CoderWanFeng pushed a commit that referenced this pull request Oct 25, 2026
…183)

补上 #183 遗漏的父包注册,使 `from skills.file import batch_rename_by_excel` 可用,
与 skills/file 下其余 skill(replace4filename / get_files 等)的导出规范保持一致。
@CoderWanFeng

Copy link
Copy Markdown
Owner

感谢贡献!🎉 你的 PR 已合入 develop。

合入 commit:

验证情况:

  • 你自带的 8 条用例全部通过(dry_run 不改文件、目标已存在时跳过不覆盖、缺文件/缺列分别抛 FileNotFoundError / ValueError、缺失原文件被归入 missing)
  • 既有测试集无回归(与合入前基线逐条比对一致)

我补了一处小改动(dd6f543):你在 skills/file/batch_rename_by_excel/ 下新增了 skill 子目录,但没有在父包 skills/file/__init__.py 里注册。结果是 from skills.file.batch_rename_by_excel import batch_rename_by_excel 可用,但 from skills.file import batch_rename_by_excel 会报 ImportError——这跟 replace4filename、get_files 等同目录 skill 的导出方式不一致。我按现有规范补了 from .batch_rename_by_excel import batch_rename_by_excel 和对应的 __all__ 条目。如果你希望这条注册由你自己提交,可以把 dd6f543 revert 掉重新提。

几点很欣赏的地方:

  • __init__.py 里用 try/except 兜底,并明确说明 contributors/ 目录不随 pip install python-office 发布(因为 setup.cfg 的 packages = find: 只收集带 __init__.py 的目录)——这个坑很多贡献者不知道,你直接处理了
  • 返回值设计了 renamed / skipped / missing / conflicts 四个键,dry_run 参数让用户能先预览再执行,安全意识到位
  • SKILL.md 里主动对比了与 replace4filename 的区别,方便用户选型
  • docstring 是中英对照的风格,与仓库其他模块一致

期待你继续贡献!

CoderWanFeng pushed a commit that referenced this pull request Oct 26, 2026
发布 1.1.0:合入 #183 / #184 / #185 三个 PR,以及自 1.0.6 以来的全部修复。

主要内容:
- feat(cli): 第一波 CLI 实现 —— `po` 命令 (#184)
- feat(contributors): 5 个 Excel 数据核对与整理方法 (#185)
- feat(contributor): batch_rename_by_excel (#183)
- fix(excel): 拆分时保留 0 值 (#180)
- fix(finance): t0 的 Decimal/float TypeError (#181)
- fix(excel): 拆分输出工作簿有效性 (#182)
- 及其他 1.0.6 以来的修复与文档更新
- 合并 main 独有的 #141 / #155

版本号: 1.0.6 → 1.1.0
CoderWanFeng pushed a commit that referenced this pull request Oct 26, 2026
## 问题

6 个贡献者 Skill 的 `__init__.py` 用 try/except 兜底处理「contributors 目录
不随包发布」的情况,但写法是:

    except ImportError as _import_error:
        def xxx(*args, **kwargs):
            raise ImportError('...') from _import_error

Python 3 在 except 块结束后会自动 `del` 掉 `as` 绑定的异常名,闭包里引用
的 `_import_error` 因而是未定义名字。在真实的 pip 安装环境(contributors
目录不存在)下调用这些 Skill,实际抛出的是:

    NameError: name '_import_error' is not defined

而不是作者预期的、带有明确指引的 ImportError 提示。

## 影响范围

- skills/file/batch_rename_by_excel(#183)
- skills/excel/excel_diff、excel_dedupe、excel_column_stats、
  excel2json、excel2csv(#185)

## 修复

把异常对象存到模块级变量 `_CONTRIBUTORS_IMPORT_ERROR`,闭包引用该变量,
既修正了 NameError,也保留了原始 ImportError 作为 `__cause__` 异常链。

## 回归覆盖

新增 tests/test_code/test_contributors_skill_fallback.py,显式 mock 掉
contributors 导入来触发兜底分支。该分支在源码仓库内永远不会自然触发,
所以常规测试抓不到这个 bug——回退修复后新测试 12 个 subtest 全部失败。

Co-authored-by: my12yyds <my12yyds@users.noreply.github.com>
CoderWanFeng pushed a commit that referenced this pull request Oct 26, 2026
修复 1.1.0 打包后发现的兜底分支缺陷:6 个贡献者 Skill 在 pip 安装环境下
调用时抛 NameError 而非预期的 ImportError(#183 / #185 引入)。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants