calibre,一站式的电子书籍管理软件,提供元信息整理、格式转换、等等,刚发现的是很是高兴,给电子书籍管理带来了方便。但是一个致命的原因——导入的中文电子书籍无法保存为中文路径和中文名,我用Everythin搜索的时候很不方便
本项目地址:snomiao/calibre-utf8-path
上手教程 – 如何将我的书库从拼音目录切换至中文命名
警告:本补丁未经全面测试,作以下操作前替换前请先备份你的书库!
注意:Calibre 5.2.0 以上安装后没有 pylib.zip 故以下方法无用,如果你有办法,请提 issue…
第一步,Calibre 5.1.0 pylib.zip 直接替换懒人包
对于 Calibre 5.1.0 的用户可以直接下载雪星修改打包好的 (点击传送下载对应版本:)Releases – pylib.zip
然后替换掉 C:\Program Files\Calibre2\app\pylib.zip
即可。 或者替换掉 C:\Program Files (x86)\Calibre2\app\pylib.zip
即可。
然后重启 Calibre。
第二步,将书库里的批量重命名
再确认一遍你备份过你的书库,以下操作不可逆且有损坏书库的风险。
打开书库 按 Ctrl + A 选择你的所有书,点 Edit Metadata (或编辑元数据),切到第2个标签页批量替换。字段点title,查找内容和替换内容都填1234,最后点 Apply (应用)
然后目录和文件名就全变成中文,接下来就可以试试用 Listary 或 Everything 搜索你的书库。
注意事项
- 记得备份
- 确认你的文件系统支持 utf8 文件名
- 目前仅在 Calibre 的 Win32位 及 Win64位 版本试过,mac 和 linux 未测试,欢迎 pr。
- 此操作不影响书库本身的兼容性,如果想切换回拼音命名的话,重装 Calibre 再批量重命名一遍即可。
- 遇到问题请在 issue 反馈。
自行编译 – 了解如何让 Calibre 使用中文目录名
准备环境
- 安装最新版 Calibre,点击进入下载页面:calibre – Download for Windows 或直接下载 calibre-latest.msi
- 下载最新源码包 calibre-latest.tar.xz
修改源码
解压 上述源码包,找到 src\calibre\db\backend.py
,打开。
主要做 2 件事
- 定义自己的文件名转换函数
- 替换掉原来的 ascii_filename
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<span class="pl-c"># src\calibre\db\backend.py</span> <span class="pl-c"># ...</span> <span class="pl-c"># 定义自己的文件名转换函数</span> <span class="pl-k">import</span> <span class="pl-s1">re</span> <span class="pl-k">def</span> <span class="pl-en">safe_filename</span>(<span class="pl-s1">filename</span>): <span class="pl-k">return</span> <span class="pl-s1">re</span>.<span class="pl-en">sub</span>(<span class="pl-s">r"[\/\\\:\*\?\"\<\>\|]"</span>, <span class="pl-s">"_"</span>, <span class="pl-s1">filename</span>) <span class="pl-c"># 替换为下划线</span> <span class="pl-c"># ... 找到 construct_path_name 和 construct_file_name 把文件名转换函数换成自己的</span> <span class="pl-k">def</span> <span class="pl-en">construct_path_name</span>(<span class="pl-s1">self</span>, <span class="pl-s1">book_id</span>, <span class="pl-s1">title</span>, <span class="pl-s1">author</span>): <span class="pl-c"># ...</span> <span class="pl-c"># author = ascii_filename(author)[:l]</span> <span class="pl-c"># title = ascii_filename(title.lstrip())[:l].rstrip()</span> <span class="pl-s1">author</span> <span class="pl-c1">=</span> <span class="pl-en">safe_filename</span>(<span class="pl-s1">author</span>) <span class="pl-s1">title</span> <span class="pl-c1">=</span> <span class="pl-en">safe_filename</span>(<span class="pl-s1">title</span>) <span class="pl-c"># ...</span> <span class="pl-k">def</span> <span class="pl-en">construct_file_name</span>(<span class="pl-s1">self</span>, <span class="pl-s1">book_id</span>, <span class="pl-s1">title</span>, <span class="pl-s1">author</span>, <span class="pl-s1">extlen</span>): <span class="pl-c"># ...</span> <span class="pl-c"># author = ascii_filename(author)[:l]</span> <span class="pl-c"># title = ascii_filename(title.lstrip())[:l].rstrip() extlen)</span> <span class="pl-s1">author</span> <span class="pl-c1">=</span> <span class="pl-en">safe_filename</span>(<span class="pl-s1">author</span>) <span class="pl-s1">title</span> <span class="pl-c1">=</span> <span class="pl-en">safe_filename</span>(<span class="pl-s1">title</span>) <span class="pl-c"># ...</span> |
使用 Python 3 编译修改后的 Calibre 源码,并替换进 pylib.zip 里
1 2 |
<span class="pl-k">cd</span> calibre-* python -O -m py_compile src\calibre\db\backend.py |
注:在 5.0 版本之前使用 Python 2.7 编译为 .pyo 文件
把 src\calibre\db\__pycache__\backend.cpython-38.opt-1.pyc
重命名为 backend.pyc
然后在 C:\Program Files\Calibre2\app\pylib.zip
里找到对应文件并替换进去
基于以上原理实现的自动化工作流
准备环境:wsl 和 Python 3.8 然后打开命令行窗口一行行执行以下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<span class="pl-c"><span class="pl-k">REM</span> 下载本项目</span> git clone https://github.com/snomiao/calibre-utf8-path <span class="pl-k">cd</span> calibre-utf8-path <span class="pl-c"><span class="pl-k">REM</span> 安装基本工具(用于下载和修改压缩包)</span> <span class="pl-k">wsl</span> sudo apt install axel zip <span class="pl-c"><span class="pl-k">REM</span> 下载calibre最新64位安装包并安装(己装可跳过)</span> <span class="pl-k">wsl</span> axel -n <span class="pl-c1">8</span> -o calibre64.msi https://calibre-ebook.com/dist/win64 ./calibre64.msi <span class="pl-c"><span class="pl-k">REM</span> 下载calibre最新源码并解压</span> <span class="pl-k">wsl</span> axel -n <span class="pl-c1">8</span> -o src.tar.xz https://calibre-ebook.com/dist/src <span class="pl-k">wsl</span> tar -xvf src.tar.xz <span class="pl-k">del</span> src.tar.xz <span class="pl-c"><span class="pl-k">REM</span> 进入目录;自动修改源码</span> <span class="pl-c"><span class="pl-k">REM</span> move calibre-* calibre-src</span> <span class="pl-k">cd</span> calibre-* python ../modify_backend.py <span class="pl-c"><span class="pl-k">REM</span> 使用 python3.8 编译并把结果转到 pylib 对应目录</span> python -O -m py_compile src\calibre\db\backend_utf8.py <span class="pl-k">move</span> /Y src\calibre\db\__pycache__\backend_utf8.cpython-38.opt-1.pyc src\calibre\db\backend.pyc <span class="pl-k">robocopy</span> src\calibre\db\ pylib_patch\calibre\db\ backend.pyc <span class="pl-c"><span class="pl-k">REM</span> 注:以下指令需要管理员权限运行</span> <span class="pl-c"><span class="pl-k">REM</span> 先备份,然后拷贝pylib.zip,编译好的文件替换进去,再替换回去</span> <span class="pl-k">copy</span> /-Y <span class="pl-s"><span class="pl-pds">"</span>C:\Program Files\Calibre2\app\pylib.zip<span class="pl-pds">"</span></span> .\pylib.backup.zip <span class="pl-k">robocopy</span> <span class="pl-s"><span class="pl-pds">"</span>C:\Program Files\Calibre2\app<span class="pl-pds">"</span></span> .\pylib_patch\ pylib.zip <span class="pl-c"><span class="pl-k">REM</span> 把编译好的东西压进包里</span> <span class="pl-k">cd</span> pylib_patch <span class="pl-k">wsl</span> chmod <span class="pl-c1">777</span> pylib.zip <span class="pl-k">wsl</span> zip -ur pylib.zip calibre/ <span class="pl-c"><span class="pl-k">REM</span> 替换回去</span> <span class="pl-k">robocopy</span> .\ <span class="pl-s"><span class="pl-pds">"</span>C:\Program Files\Calibre2\app<span class="pl-pds">"</span></span> pylib.zip <span class="pl-c"><span class="pl-k">REM</span> 完成</span> |