Skip to main content

Create PDF bookmarks automatically using heuristics, with CLI and async tkinter GUI

Project description

TocSmith

为 PDF 添加目录书签的实用工具,支持命令行与简易 GUI。通过“手动粘贴目录文本 + 页码偏移”的方式生成 PDF 书签(大纲/Outline)。

  • 运行环境:Python 3.9+
  • 依赖:pypdf(写书签)
  • 提供方式:CLI、Tk GUI、Python API

功能概览

  • 手动粘贴目录文本(每行以书中页码结尾),自动解析标题、页码与层级(1..6)
  • 支持页码偏移(实际页码 - 书籍页码),用于扫描件/前置页差异
  • 编号前缀会被保留到标题中:如 第1章1.1 将出现在最终书签标题里
  • 支持行首星号标记:允许输入 *1.1 Title* 1.1 Title,输出统一为 *1.1 Title
  • 将条目以父子层级写入 PDF 书签
  • 提供 CLI 与 GUI;亦可通过 Python API 使用

快速开始

安装与运行(uv 推荐)

本仓库使用 uv 管理与分发工具。

  1. 通过uv安装命令行工具(推荐):
uv tool install tocsmith
# 安装后可直接使用:
tocsmith --help
tocsmith-gui
  1. 使用 pip 安装(备选):
pip install tocsmith

# 现在可直接使用:
tocsmith --help
tocsmith-gui
  1. 本地开发
git clone https://github.com/wesleyel/pdf-bookmark.git
cd pdf-bookmark
uv sync

uv tool install . --reinstall

tocsmith --help
tocsmith-gui

命令行使用(CLI)

tocsmith --help

通过 TOML 批量执行(自定义格式)

支持通过 TOML 配置批量执行多个任务。相对路径均以配置文件所在目录为基准;还可以通过 defaults.input_prefixdefaults.output_prefix 设定输入/输出根目录。

示例 config.toml

[defaults]
# global page offset
page_offset = 0
# global minimum length
min_len = 3

# input folder
input_prefix = "input"
# output folder
output_prefix = "output"
# output file name append
output_suffix = ".bookmarked.pdf"

[[tasks]]
# input file name. relative to input_prefix
input_file = "book1.pdf"
toc = """
第一章 绪论 1
1.1 引言 3
1.2 数学分析的基本概念 5
"""
page_offset = 10
min_len = 2

运行:

tocsmith --config config.toml

说明:

  • defaults 中的 page_offsetmin_len 可被每个任务覆盖。
  • input_prefix 用于解析任务中的 input_fileoutput_prefix 为输出目录根。
  • 输出文件名为 {stem}{output_suffix},其中 stem 来源于 input_file
  • 任务可直接内联 toc 文本;也兼容 toc_file 指定外部文件。

图形界面(GUI)

提供一个基于 Tk 的简易界面,便于在桌面环境下操作:

tocsmith-gui
# 或
uv run python -m tocsmith.gui

基本流程:

  • 选择输入 PDF
  • 可选:修改输出路径
  • 在 “TOC text” 中粘贴目录文本;在 “Page Offset” 填写偏移(实际 - 书籍)
  • 点击 “Parse TOC Text” 查看解析结果
  • 点击 “Generate” 生成带书签的 PDF

提示:Linux 上若缺少 tkinter,可通过安装系统包启用(例如 Debian/Ubuntu:sudo apt-get update && sudo apt-get install -y python3-tk)。

开发与测试

  • 代码检查与测试:
uv tool install .  # 安装命令,便于本地手动验证
uv run pytest -q
# 可选:
uv run ruff check
uv run mypy tocsmith
  • 项目结构:
tocsmith/
  core.py   # 目录解析与书签生成核心逻辑
  cli.py    # 命令行入口
  gui.py    # Tk GUI 入口
  tests/    # 单元测试(pytest)

许可证

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tocsmith-0.1.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tocsmith-0.1.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file tocsmith-0.1.0.tar.gz.

File metadata

  • Download URL: tocsmith-0.1.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for tocsmith-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4691aab9b92f9a1d1038e52fcef592cbd835858586801ff734b00bfc2849c428
MD5 c691404b6a022584f8d0b2c1ae1f21f0
BLAKE2b-256 c9aac217c944da71e025ac157d0322851352c0b05da6b32c2122c74e08a5d427

See more details on using hashes here.

Provenance

The following attestation bundles were made for tocsmith-0.1.0.tar.gz:

Publisher: release.yml on wesleyel/tocsmith

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tocsmith-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tocsmith-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for tocsmith-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3d6b3868d8838fc5174de3e2e5465c7e05f31f12bce1a03b6c2598fea45c7ad
MD5 aa4b33c6022be4ed236f72e0338b6ecc
BLAKE2b-256 88886c3690119d7f2886bf585dc41ac816bdeca61e8ec0ace625020dde86991e

See more details on using hashes here.

Provenance

The following attestation bundles were made for tocsmith-0.1.0-py3-none-any.whl:

Publisher: release.yml on wesleyel/tocsmith

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page