auto-bookmarks

Automatically add clickable tables of contents or bookmarks to files that lack navigation. Intended for cases where users want clickable outlines for PDF, EPUB, or TXT. Supports: PDF (font-size heuristics for text PDFs; OCR reverse lookup and page-offset mode for scanned PDFs), EPUB (rebuild NCX/Nav based on h1–h4), and TXT (generate bookmarked EPUB via chapter detection).

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "auto-bookmarks" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-47a41e671bf4b509abf845bb4b3b9f986cebc02e8b8d4f96d8533bddbc420942. Verify the upstream source and version against https://skillhub.cn/skills/user_dde7a223/auto-bookmarks-skill before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

skillhub.cn

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Automatically add clickable tables of contents or bookmarks to files that lack navigation. Intended for cases where users want clickable outlines for PDF, EPUB, or TXT. Supports: PDF (font-size heuristics for text PDFs; OCR reverse lookup and page-offset mode for scanned PDFs), EPUB (rebuild NCX/Nav based on h1–h4), and TXT (generate bookmarked EPUB via chapter detection).

0 Downloads

Introduction source: third_party · user_dde7a223 · 1.0.0 Reference

SkillPi interpretation

Tasks
Translation pending; see source documentation.
Audience
Translation pending; see source documentation.
Inputs and outputs
Not specified in the source documentation.
Software, accounts and paid services
Translation pending; see source documentation.
Limits and differences
Translation pending; see source documentation.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: auto-bookmarks
description: "Automatically add navigation bookmarks / table of contents to books that lack them. Use when a user wants clickable outlines or TOC added to a PDF, EPUB, or TXT file (e.g. '给这本 PDF 加目录', '这本书没有书签', '把 TXT 转成带目录的 EPUB', 'auto-generate bookmarks for this ebook'). Handles PDF (font-size heuristic outline for text-based PDFs; OCR reverse-lookup for image-only/scanned PDFs; TOC-offset mode for scanned PDFs that print a page-numbered table of contents), EPUB (rebuild NCX/Nav from h1-h4), and TXT (chapter detection to bookmarked EPUB)."
agent_created: true

auto-bookmarks

为没有书签的 PDF / EPUB / TXT 书籍自动生成可点击的目录(书签 / 大纲)。

适用场景

  • PDF 由 Word / InDesign / LaTeX / 排版软件导出,但没有内嵌 outline。
  • EPUB 缺少 NCX / Nav 导航(阅读器左侧没有目录)。
  • TXT 纯文本书,希望变成带目录、能在阅读器里跳转的 EPUB。
  • 扫描件 / 图片型 PDF(无文本层)也支持:走 add_bookmarks_scanned.py 的 OCR 反查流程(见下方「PDF(扫描件 / 图片型)— OCR 反查」)。
  • 扫描件且自带「印刷目录」(每章印了书页号)时,优先用 TOC-offset 模式 (add_bookmarks_toc_offset.py):用目录里的书页号 + 固定偏移直接换算 PDF 页, 比 OCR 逐章反查更稳(OCR 会把「第一章」误识成乱码、还会把目录页本身当成命中)。 见下方「PDF(扫描件)— TOC-offset(目录页码偏移法)」。

依赖

脚本基于现成轮子实现,需安装 Python 依赖:

# 基础(文本型 PDF / EPUB / TXT)
pip install PyMuPDF ebooklib beautifulsoup4 lxml
# 扫描件 OCR 流程额外需要
pip install pillow pytesseract
  • PDF 标题检测用 PyMuPDF(fitz)的字体大小 / 加粗 / 坐标启发式,算法思路借鉴 pdf.tocgen 与 AddTOC,写回用 doc.set_toc()。
  • 扫描件走 Tesseract OCR(pytesseract 调用),中文需装 chi_sim 语言包:
    • Windows:https://github.com/UB-Mannheim/tesseract/wiki 安装时勾选 Additional language data → chi_sim;默认读 C:/Program Files/Tesseract-OCR/tesseract.exe(用 TESSERACT_PATH 可覆盖)。
    • macOS:brew install tesseract tesseract-lang
    • Linux:apt install tesseract-ocr tesseract-ocr-chi-sim
  • EPUB / TXT 的目录生成用 ebooklib,NCX + EPUB3 Nav 的构建方式与 Calibre 自动 TOC 同出一源(基于标题扫描)。详见 references/credits.md。

使用流程

入口脚本:scripts/add_bookmarks.py。默认先预览目录树,再交互确认写入;非交互 环境(无 TTY)下不会自动写入,需显式传 --yes。

# 1) 先预览检测到的目录,核对层级与页码
python scripts/add_bookmarks.py path/to/book.pdf --preview

# 2) 确认无误后写入(交互环境会再问一次 y/N;非交互直接加 --yes)
python scripts/add_bookmarks.py path/to/book.pdf --yes

# EPUB:自动扫描 h1-h4 重建 NCX + Nav
python scripts/add_bookmarks.py path/to/book.epub --yes

# TXT:识别章节后转成带 TOC 的 EPUB
python scripts/add_bookmarks.py path/to/book.txt --title "书名" --yes

# 覆盖已存在的目录
python scripts/add_bookmarks.py book.pdf --force --yes

默认输出文件名:PDF/EPUB 为 <原名>.bookmarked.<ext>,TXT 输出 <原名>.epub; 用 --out 自定义。已存在同名文件会自动加 (1) 后缀,不会覆盖。

参数

参数 说明
--preview 仅打印检测到的目录树,不写文件
--yes 跳过确认直接写入(批量 / 非交互必备)
--force 覆盖已存在的目录(PDF outline / EPUB NCX)
--ratio F PDF 标题阈值:字号 ≥ 正文×F 视为标题,默认 1.15(调小更激进)
--title S TXT 转 EPUB 时的书名(默认取文件名)
--out P 指定输出路径
--dpi / --workers / --lang 仅扫描件 OCR 流程:分辨率(默认 150)/ 进程数(默认 6)/ 语言(默认 chi_sim)
--chapters F 仅扫描件:显式章节表文件,按序向后反查,最精准
--sections 仅扫描件:自动模式也纳入「第X节」层级
--toc F TOC-offset 模式:目录规格文件(含每章书页号),PDF 用偏移法定页
--offset N TOC-offset:手动指定偏移(PDF页 − 书页号);不指定则自动推导
--nest TOC-offset:自动推断层级(讲/部/篇/卷 → L1,章/节 → L2)

各格式处理要点

PDF(启发式 outline)

  1. 先用 doc.get_toc() 检查是否已有目录,有则提示跳过(--force 覆盖)。
  2. 提取每一行文本的字号、是否加粗、坐标。正文基准字号 = 全文档出现最多的字号。
  3. 字号 ≥ 正文×--ratio(或加粗且接近正文)的行判定为标题候选。
  4. 按字号聚类分层:相邻字号差 ≤ 1.0pt 视为同层,最大字号 = L1,最多 4 层。
  5. 生成 [level, title, page, Point(0, y)] 写回 outline,点击书签精确滚到标题位置。

若检测不准(层级混乱 / 漏标),调 --ratio:层级过多调大,漏标标题调小。

PDF(扫描件 / 图片型)— OCR 反查

当 PDF 无文本层(纯图片扫描)时,add_bookmarks.py 会自动改走 scripts/add_bookmarks_scanned.py 的 OCR 流程(也可直接调用该脚本)。流程: 并行全本 OCR(Tesseract)→ 定位章节起始页 → 写书签进副本。

章节定位有两种模式,均严格向后单调搜索(命中一章后,后续章节只能在该页之后 找),避免「靠前的同名片段 / 目录页 / 页眉」抢先误命中(实测曾把第十二章误跳到 p124、 附录误跳 p22)。

自动模式(默认):正则识别每页的章节级标题(第X章/篇/卷/部、附录X、引言/ 序言/致谢/参考文献、Chapter/Appendix/Part 等),并自动跳过「目录页」——含大量章节 标题的密集页(阈值 ≥ 3 个不同标题),取每个章节首次出现的真实正文页。

python scripts/add_bookmarks_scanned.py book.pdf --preview   # 先看自动检测结果
python scripts/add_bookmarks_scanned.py book.pdf --yes       # 自动模式写入
python scripts/add_bookmarks_scanned.py book.pdf --sections --yes   # 连「第X节」也纳入

显式模式(--chapters 文件,最精准):当自动模式漏检(如随笔集、无「第X章」 结构的书)时,按你给的有序章节表逐章向后做子串反查。章节表每行:

标题
标题|别名1|别名2      # 别名用于提高 OCR 命中率(如 第十一章|昼夜节律军备竞赛)
# 这是注释
python scripts/add_bookmarks_scanned.py book.pdf --chapters ch.txt --yes

OCR 文本会缓存到 scripts/.ocr_cache/<书名>.json,改 --chapters / 加 --sections 重算时无需重新 OCR(几秒出结果)。缓存可随时删。

PDF(扫描件)— TOC-offset(目录页码偏移法,第三模式)

适用:扫描件 / 图片 PDF,且书里印了一页「印刷目录」,每章都带书页号 (如「引言 /1」「第1章 雇员和创业者有何区别? /30」)。此时最稳的不是用 OCR 去 正文反查标题(OCR 会把「第一章」误识成乱码、还会把目录页自身当成命中),而是:

  1. 用目录里的「书页号」+ 一个固定偏移,直接换算该章的 PDF 页号 (偏移 offset = 正文某章的真实 PDF 页 − 它的书页号);
  2. 只需定位 1 个正文章节即可自动推导偏移(--offset 也可手动给);
  3. 每个章节再用「窗口内 OCR 验证」兜底:验证页与偏移页一致才微调,否则信任偏移页;
  4. 自动跳过「目录页」(含大量章节名、被判为密集页),不会把目录页当成章节起始。

为什么比「逐章 OCR 反查」稳:偏移恒定(无插页时 书页号 + offset == PDF页 全本成立),彻底绕开 OCR 对正文章标题的误识,也天然避开目录页干扰。

目录规格文件格式(每行一条,# 开头为注释):

# 标题(无书页号)→ 向前 OCR 反查,适合前言/致谢等 front-matter
致谢
# 标题 | 书页号
引言|1
富爸爸创业课程 第1讲|29
第1章 雇员和创业者有何区别?|30
# 标题 | 书页号 | 层级(1/2/3)  —— 显式层级,优先级高于 --nest
第2章 有无旁观者:目标不同|56|2

也支持直接粘贴目录行(脚本会自动从行尾解析书页号):

引言 /1
第1章 雇员和创业者有何区别? /30

调用(统一入口 add_bookmarks.py 或专用脚本均可):

# 先用 --preview 看偏移推导与页码,核对无误
python scripts/add_bookmarks.py book.pdf --toc toc.txt --nest --preview
# 两层结构(讲/部/篇/卷 → L1,章/节 → L2)写入
python scripts/add_bookmarks.py book.pdf --toc toc.txt --nest --yes

# 偏移自动推导失败时才手动指定:--offset 11 表示 PDF页 = 书页号 + 11
python scripts/add_bookmarks_toc_offset.py book.pdf --toc toc.txt --offset 11 --nest --yes

实测案例:《富爸爸 辞职创业前的10堂课》(扫描件,314 页,章节为「第N讲」+ 「第N章」交替)。自动模式正则只认「章/篇/卷/部」会漏掉全部「讲」,且正文「第一章」 被 OCR 误识;改用 TOC-offset 后,用目录书页号 + 偏移 11 直接定页,自动产出 22 条 两级书签(致谢 + 引言 / 第1~10讲 各挂其第N章子项),页码严格递增、定位精准。 规格文件见 examples/toc_富爸爸.txt(若已生成)。

EPUB(重建 NCX / Nav)

  1. 按 spine 阅读顺序遍历文档,扫描 h1-h4 标题;缺 id 的标题自动补锚点。
  2. 按标题层级构建嵌套 book.toc,生成 EpubNcx() + EpubNav() 写回。
  3. 已存在 NCX/Nav 时提示跳过(--force 删除旧目录后重建)。

TXT(章节检测 → 带 TOC 的 EPUB)

识别的中文章节:第X章/回/卷/部/篇(L1)、第X节(L2); 英文:Chapter/Part/Book/Volume(L1)、Section(L2); 数字编号:1. Title / 1.1 Title / 2.3.4(按点数定层级,最多 4 层)。 开头无标题的段落归为「前言」。按章切分生成 XHTML 章节,构建带导航 TOC 的 EPUB。

注意事项

  • 扫描件 / 图片型 PDF 现已支持(OCR 反查,见上方章节);只有 OCR 质量太差、 或书本身无清晰章节标题结构时才会漏检,此时用 --chapters 显式给出章节表。
  • PDF 页码使用文档实际页序(非页标签),书签定位到标题纵坐标。
  • 标题跨多行换行时可能拆成多条,长篇书籍建议预览后人工核对。
  • 输出为新文件,原文件不会被修改。

Current listing version

1.0.0

Verify the source revision ↗

Safety and assessment details

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value92/100

Highlights

  • 解决用户为 PDF/EPUB/TXT 添加可点击目录的核心痛点
  • 支持文本型 PDF 启发式检测、扫描件 OCR 反查、TOC-offset 偏移法三种模式
  • 覆盖 EPUB 重建 NCX/Nav 和 TXT 转带目录 EPUB 场景
Trigger Precision95/100

Highlights

  • 触发词覆盖典型用户表达(如“给这本 PDF 加目录”、“这本书没有书签”)
  • 明确区分 PDF/EPUB/TXT 三大格式场景,边界清晰
Structure and Standards90/100

Areas to improve

  • 文档内容较长 Reported severity: Minor

    SKILL.md 正文约 180 行,超出理想长度

    Recommendation: 可考虑将部分技术细节移至独立文档并通过链接引用

  • description 重复 Reported severity: Minor

    frontmatter 和正文开头均包含 description 内容

    Recommendation: 保留一处即可,避免冗余

Highlights

  • 结构清晰:适用场景→依赖→使用流程→各格式处理→注意事项
  • 提供完整参数表和端到端调用示例
  • frontmatter 元数据完整
Execution Reliability88/100

Highlights

  • 依赖库明确(PyMuPDF、ebooklib、Tesseract)并给出安装命令
  • 提供 --preview 预览模式和 --yes 显式确认机制,防止误操作
  • 输出文件自动防覆盖(加 (1) 后缀),原文件不被修改
  • OCR 结果缓存复用,提升重复执行效率
Evidence Verifiability (mean of the first four)91/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

Domains or addresses referenced in the document

These addresses may be documentation links or examples, not an approved network allowlist. Verify actual destinations and outgoing data before use.

  • github.com

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Translation pending; see source documentation.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.