pagelabels
用于操作 PDF 页面标签的 Python 工具。
PDF 中一个实用但很少使用的功能是能够使用 自定义的页面命名方案。这使得可以从 任意指定的页码开始 PDF,而不是从 1 开始,可以为长 PDF 的每个部分 重新开始页码编号,或者为特定页面 分配一个名称。

Addpagelabels 工具
PDF 文件可以包含一个或多个页码编号方案。 每个方案都有一个起始页,指定该方案应生效的页面。 所有后续页面都将受该方案影响, 直到遇到另一个页码编号方案为止。 此工具允许在 PDF 文件中添加、删除和更新页码编号方案。
安装
依赖项
如果您尚未安装 pip,请先安装:
在 Linux 上
$ sudo apt install python3-pip
在 MacOS 上
安装 brew,然后安装 python:
brew install python
在 Windows 上
安装 WSL 然后按照 Linux 的说明操作。
脚本
安装 pagelabels-py :
python3 -m pip install --user --upgrade pagelabels
如何使用
向 PDF 添加新的页码索引
此操作读取文件 /tmp/test.pdf,
并创建其副本,添加新的页码标签,
同时不删除可能已存在的标签。
新索引将从 PDF 的第 1 页开始生效,
由大写罗马数字组成,前缀为字符串 "Intro ",
并从 "V" 开始。
页码将为:"Intro V"、"Intro VI"、"Intro VII"、...
python3 -m pagelabels --startpage 1 --type "roman uppercase" --prefix "Intro " --firstpagenum 5 --outfile /tmp/new.pdf /tmp/test.pdf
打印用法信息
python3 -m pagelabels -h
这应该会打印:
usage: pagelabels [-h] [--outfile out.pdf] [--delete | --update]
[--startpage STARTPAGE]
[--type {arabic,roman lowercase,roman uppercase,letters lowercase,letters uppercase,none}]
[--prefix PREFIX] [--firstpagenum FIRSTPAGENUM]
[--load other.pdf]
file.pdf
Add page labels to a PDF file
positional arguments:
file.pdf the PDF file to edit
optional arguments:
-h, --help show this help message and exit
--outfile out.pdf, -o out.pdf
Where to write the output file
--delete delete the existing page labels
--update change all the existing page numbering schemes instead
of adding a new one
--startpage STARTPAGE, -s STARTPAGE
the index (starting from 1) of the page of the PDF
where the labels will start
--type {arabic,roman lowercase,roman uppercase,letters lowercase,letters uppercase,none}, -t {arabic,roman lowercase,roman uppercase,letters lowercase,letters uppercase,none}
type of numbers: arabic = 1, 2, 3, roman = i, ii, iii,
iv, letters = a, b, c, none = (number is empty)
--prefix PREFIX, -p PREFIX
prefix to the page labels
--firstpagenum FIRSTPAGENUM, -f FIRSTPAGENUM
number to attribute to the first page of this index
--load other.pdf copy page number information from the given PDF file
从 PDF 中删除现有的页码标签
python3 -m pagelabels --delete file.pdf
将一个 PDF 的页码标签复制到另一个 PDF
以下操作将获取 source.pdf 的页码标签方案,
并将其应用于 target.pdf :
python3 -m pagelabels --load source.pdf target.pdf
完整示例:创建具有多种不同页码样式的 PDF
假设我们有一个名为 my_document.pdf 的 PDF,共有 12 页。
- 第 1 至 4 页应标记为
Intro I至Intro IV。 - 第 5 至 9 页应标记为
2至6。 - 第 10 至 12 页应标记为
Appendix A至Appendix C。
我们可以发出以下命令列表:
python3 -m pagelabels --delete "my_document.pdf"
python3 -m pagelabels --startpage 1 --prefix "Intro " --type "roman uppercase" "my_document.pdf"
python3 -m pagelabels --startpage 5 --firstpagenum 2 "my_document.pdf"
python3 -m pagelabels --startpage 10 --prefix "Appendix " --type "letters uppercase" "my_document.pdf"
更新现有页码
假设我们有一个 PDF,其页面命名为 10、11、12、A、B、C,
我们希望为标签添加前缀,同时保留现有的自定义
页码偏移和样式。我们可以使用 pagelabels 的 --update 选项来实现:
python3 -m pagelabels --update --prefix "EX-" my_document.pdf
这将把现有标签更新为 EX-10、EX-11、EX-12、EX-A、EX-B、EX-C。
警告
pagelabels-py 内部使用 pdfrw,它只能写入 PDF 版本 1.3。如果您的 PDF 使用了与 PDF 1.3 不兼容的功能,在使用 pagelabels-py 后,您可能会看到它无法正确渲染。
作为 python 库使用
本项目可以作为 python 库使用。 请参阅 pagelabels 在 python 包索引。