獲取和設置pdf目錄

前言

  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》的【下載鏈接解壓密碼://www.cnblogs.com/janbar/】放出來,大家自己練習吧。