git权威指南 pdf_获取和设置pdf目录

本文介绍了使用pdf-toc命令行工具快速获取和设置PDF目录的方法。通过执行特定命令,可以获取PDF的toc格式目录,包括文本和JSON两种格式,方便进一步编辑或自动化处理。此外,还展示了如何利用该工具设置PDF目录,只需提供正确的目录格式文件即可。此工具适用于需要批量处理PDF目录的场景,极大地提高了效率。
摘要由CSDN通过智能技术生成
76755c5a258c9667c967e5b249e7b9a5.png

前言

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

工具

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

pdf-toc -husage: 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://545c.com/file/28844697-468213579,解压密码:https://www.cnblogs.com/janbar/】放出来,大家自己练习吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值