Features
对pdf文件,进行增加、插入、删除、旋转、切割、合并等操作。

Download
主页:https://github.com/MrLeeh/pdftools
下载:git clone https://github.com/MrLeeh/pdftools.git

Installation
只支持python3的环境,因此需要安装python3。

root [~] git clone https://github.com/MrLeeh/pdftools.git
root [~] cd pdftools
root [~] python3 setup.py install

安装完成后,就可以直接使用了,在根目录有所有执行程序。

pdfadd.py
将一个pdf的指定连续页面,添加到另一个pdf后面。

usage: pdfadd.py [-h] [--version] [-p PAGES [PAGES ...]] [-o OUTPUT]
                 dest source

Add pages from a source file to an output PDF file. If the output file does
not exist a new file will be created.

positional arguments:
  dest                  destination pdf file
  source                pdf source file

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -p PAGES [PAGES ...], --pages PAGES [PAGES ...]
                        list of pages to add to the output. Examples: 5 1-9 1-
                        -9
  -o OUTPUT, --output OUTPUT
                        name of the output file, if None the destinationfile
                        will be overwritten

pdfinsert.py
将一个pdf的指定连续页面,插入到另一个pdf指定位置。

usage: pdfinsert.py [-h] [--version] [-o OUTPUT] [-p PAGES [PAGES ...]]
                    [-i INDEX]
                    dest source

Insert pages of one file in another.

positional arguments:
  dest                  destination pdf file
  source                source pdf file

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -o OUTPUT, --output OUTPUT
                        filename of the output files, if None given dest will
                        be used as output
  -p PAGES [PAGES ...], --pages PAGES [PAGES ...]
                        list of page numbers (start with 1) which will be
                        inserted, if None all pages will be rotated (default),
                        Examples: 5 1-9 1- -9
  -i INDEX, --index INDEX
                        page number (start with 1) of destination file where
                        the pages will be inserted, if None they will be added
                        at the end of the file

pdfremove.py
删除pdf的指定连续页面。

usage: pdfremove.py [-h] [--version] -p PAGES [PAGES ...] [-o OUTPUT] source

Remove pages from a PDF file.

positional arguments:
  source                pdf source file

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -p PAGES [PAGES ...], --pages PAGES [PAGES ...]
                        list of pages to remove from file. Examples: 5 1-9 1-
                        -9
  -o OUTPUT, --output OUTPUT
                        name of the output file, if None the source file will
                        be overwritten

pdfrotate.py

旋转pdf指定连续页面90度(可逆时针、可顺时针),建议全页面旋转,否则可能存在问题。

usage: pdfrotate.py [-h] [--version] [-c] [-p PAGES [PAGES ...]] [-o OUTPUT]
                    input

Rotate the pages of a PDF files by 90 degrees.

positional arguments:
  input                 input file

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -c                    rotate pages counterclockwise
  -p PAGES [PAGES ...], --pages PAGES [PAGES ...]
                        list of page numbers which will be rotated, if None
                        all pages will be rotated (default). Examples: 5 1-9
                        1- -9
  -o OUTPUT, --output OUTPUT
                        name of the output file, if None the source filewill
                        be overwritten

pdfsplit.py
将一个pdf文件可以拆分为多个文档。 新文档根据-o参数命名,页码和文件结束pdf自动添加到名称。
默认不传即可将每一页都进行切割,-q参数经测试,不好用。-s参数表示分割成多少个pdf。

usage: pdfsplit.py [-h] [--version] [-o OUTPUT] [-s STEPSIZE]
                   [-q SEQUENCE [SEQUENCE ...]]
                   input

Split a PDF file in multiple documents.

positional arguments:
  input                 input file that shall be splitted

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -o OUTPUT, --output OUTPUT
                        filename of the output files
  -s STEPSIZE, --stepsize STEPSIZE
                        defines how many pages are packed in each output file
  -q SEQUENCE [SEQUENCE ...], --sequence SEQUENCE [SEQUENCE ...]
                        sequence of numbers describing how many pages to put
                        in each outputfile

pdfmerge.py

将多个文件,按照传参顺序,合并成一个文件。

usage: pdfmerge.py [-h] -o OUTPUT [-d] inputs [inputs ...]

Merge the pages of multiple input files in one output file.

positional arguments:n
  inputs                list of input files

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        filename of the output file
  -d, --delete          delete input files after merge

Demo
pdf下载地址:
http://static.ranshy.com/code/python_pdf_test.zip

1、2、3、4分别为四个pdf文档,每个文档只包含一个数字,方便测试和查看。

将2追加到1后面,生成12:
pdfadd.py 1.pdf 2.pdf -o 12.pdf

将12、3、4合并,生成1234:
pdfmerge.py 12.pdf 3.pdf 4.pdf -o 1234.pdf

将1234的第2页和第3页,追加到12后面,生成1223.pdf:
pdfadd.py 12.pdf 1234.pdf -p 2-3 -o 1223.pdf

将3插入到1223的第2页位置,生成13223.pdf:
pdfinsert.py 1223.pdf 3.pdf -i 2 -o 13223.pdf

将1223的第2页和第3页,插入到1234的第4页位置处,生成123224.pdf:
pdfinsert.py 1234.pdf 1223.pdf -i 4 -p 2-3 -o 123224.pdf

将1234的第4页,插入到12的中间位置,生成142:
pdfinsert.py 12.pdf 1234.pdf -i 2 -p 4 -o 142.pdf

删除142的第1页,生成42.pdf:
pdfremove.py 142.pdf -p 1 -o 42.pdf

删除13223的中间所有页面,生成13.pdf:
pdfremove.py 13223.pdf -p 2-4 -o 13.pdf

旋转1234所有页面,顺时针90度,生成1234_shunshi.pdf:
pdfrotate.py 1234.pdf -o 1234_shunshi.pdf

旋转1234所有页面,逆时针90度,生成1234_nishi.pdf:
pdfrotate.py 1234.pdf -o 1234_nishi.pdf

拆分142.pdf,拆分成一页一页,并保存在142目录下面,共3个pdf(142.pdf_1.pdf、142.pdf_2.pdf、142.pdf_3.pdf):
mkdir 142
python3 /opt/python/pdftools/pdfsplit.py 142.pdf -o 142/142.pdf

拆分13223.pdf,每2个1页,共拆成3页(13一页、22一页、3一页):
mkdir 13223
python3 /opt/python/pdftools/pdfsplit.py 13223.pdf -s 2 -o 13223/13223.pdf

最后,分享一个在线的pdf操作工具,主要是拆分和合并,还是蛮好用的:
https://online2pdf.com/