前言

  1. 获取pdf目录:主要场景是为了拿到目录信息做编辑,从pdf软件里面一个个复制那肯定费时费力。
  2. 设置pdf目录:主要场景,扫描文件或拍多张照片等制作的pdf文件是没有目录。还有就是网上很多扫描版的pdf加上目录很方便。

工具

在网上搜索一波,找到了【pdf-toc】这个命令行工具,可以实现获取pdf目录,以及设置pdf目录。
感觉很方便,只是需要python >= 3.6的环境,然后执行pip install pdf-toc进行安装。该命令行的帮助文档如下:

pdf-toc -h
usage: pdf-toc [-h] [--version] [--show-toc {json,toc}] [-t TOC] [-d DEST]
               [-T {json,toc}] [-f] [-m]
               source

pdf ToC modifier.

positional arguments:
  source                source pdf file directory

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --show-toc {json,toc}
                        print the toc info of the source file and exit
  -t TOC, --toc TOC     toc info used to embed in the result file. leave it
                        empty to read toc from stdin
  -d DEST, --dest DEST  destination directory for result file
  -T {json,toc}, --type {json,toc}
                        specify format of ToC file. leave it empty to let the
                        tool determine the format, (from file suffix)
  -f, --force           overwrite dist file if it exist
  -m, --modify          modified the original file instead of create a new one

获取pdf目录

获取toc格式目录

执行命令pdf-toc --show-toc toc MongoDB_dst.pdf可以得到简单的文本目录格式。
下面只截取部分目录信息,可以看到结构为【缩进 + 目录名 + 空格 + 页码】,前面的缩进一般为4个空格,为4的倍数表示第几级目录。
该方式适用于得到要修改的目录,因为后面设置目录可以用下面这种格式的文件。

目录    14
第一部分 MongoDB介绍    24
    第1章 MongoDB简介    26
        1.1 易于使用    26
        1.2 易于扩展    27
        1.3 丰富的功能    27
        1.4 卓越的性能    28
        1.5 小结    28
    第2章 MongoDB基础知识    30
        2.1 文档    30
        2.2 集合    31
            2.2.1 动态模式    31
            2.2.2 命名    32
        2.3 数据库    33
        2.4 启动MongoDB    34
        2.5 MongoDB shell简介    35

获取json格式目录

执行命令pdf-toc --show-toc json MongoDB_dst.pdf可以得到简单的文本目录格式。
下面只截取部分目录信息,可以看到结构为【目录等级 + 目录名 + 页码 + 种类信息】。
该方式得到的数据最适合用来写代码解析拿去为所欲为

[
    [1, "目录   ", 14, {"kind": 1, "xref": 0, "page": 13, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [1, "第一部分 MongoDB介绍   ", 24, {"kind": 1, "xref": 0, "page": 23, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [2, "第1章 MongoDB简介   ", 26, {"kind": 1, "xref": 0, "page": 25, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [3, "1.1 易于使用   ", 26, {"kind": 1, "xref": 0, "page": 25, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [3, "1.2 易于扩展   ", 27, {"kind": 1, "xref": 0, "page": 26, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [3, "1.3 丰富的功能   ", 27, {"kind": 1, "xref": 0, "page": 26, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [3, "1.4 卓越的性能   ", 28, {"kind": 1, "xref": 0, "page": 27, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [3, "1.5 小结   ", 28, {"kind": 1, "xref": 0, "page": 27, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [2, "第2章 MongoDB基础知识   ", 30, {"kind": 1, "xref": 0, "page": 29, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [3, "2.1 文档   ", 30, {"kind": 1, "xref": 0, "page": 29, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [3, "2.2 集合   ", 31, {"kind": 1, "xref": 0, "page": 30, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [4, "2.2.1 动态模式   ", 31, {"kind": 1, "xref": 0, "page": 30, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}],
    [4, "2.2.2 命名   ", 32, {"kind": 1, "xref": 0, "page": 31, "to": {"__type__": "Point", "x": 72.0, "y": 0.0}, "zoom": 0.0}]
}

设置pdf目录

设置pdf目录很简单,需要制作获取toc格式目录中得到的那种格式文件。特别注意缩进为4个空格,且文件要为utf-8编码。
然后执行pdf-toc -t MongoDB.txt -d MongoDB_dst.pdf MongoDB_src.pdf就可以得到一个带目录的文件MongoDB_dst.pdf,是不是很方便。

结语

    本来需求很简单就是要获取pdf目录,百度了好一些Python库都没咋看到获取pdf目录的现成代码,我也懒得看那些库的方法文档,肯定是有方法的。但是懒驱动我找到了这个工具,我制作《MongoDB权威指南(第2版.pdf》的pdf目录文件还是花了我好多时间,眼睛都快看瞎了,不过为了方便我自己学习,我还是弄好了。在这里我也把转换前的目录文件和《MongoDB权威指南(第2版.pdf》的【下载链接解压密码:https://www.cnblogs.com/janbar/】放出来,大家自己练习吧。

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/janbar/p/13880346.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!