Pdfslice

A tool for slicing (or rearranging) pages in a PDF document.

Author: Dinu Gherman <gherman@darwin.in-berlin.de>
Homepage:http://www.dinu-gherman.net/
Version: Version 0.4.1
Date: 2008-09-17
Copyright: GNU Public Licence v3 (GPLv3)

About

Pdfslice is a Python command-line tool and module for slicing and rearranging pages of a PDF document. Using it you can pick single pages or ranges of pages from a PDF document and store them in a new PDF document. To do this you describe these pages with the simple Python slice notation, e.g. 0:10 for the first ten pages, -10:0 for the last ten pages, 0::2 for all even pages, -1::-1 for all pages in reversed order, etc.

Basically, pdfslice wrapps pyPdf, a package written by Mathieu Fenniak which contains the needed functionality in its core, but does not provide a simple method of using it easily from the command-line or from a Python module.

Features

Examples

You can use pdfslice as a Python module e.g. like in the following interactive Python session (the function's signature might still change a bit in the future):

>>> from pdfslice import slicePages
>>>
>>> slicePages("file.pdf", [slice(0, 1, None)])    # i.e. [0]
written: file-sliced.pdf
>>> slicePages("file.pdf", [slice(None, None, 2)]) # i.e. [::2]
written: file-sliced.pdf

In addition there is a script named pdfslice, which can be used more easily from the system command-line like this (you can see many more examples when typing pdfslice -h on the command-line):

$ pdfslice 0 file.pdf
written: file-sliced.pdf
$ pdfslice ::2 file.pdf
written: file-sliced.pdf
$ pdfslice -p -o "%(base)s-p%(indices)s%(ext)s" 3-5 /path/file[12].pdf
written: file1-p3-5.pdf
written: file2-p3-5.pdf

Installation

There are two ways to install pdfslice, depending on whether you have the easy_install command available on your system or not.

1. Using easy_install

With the easy_install command on your system and a working internet connection you can install pdfslice with only one command in a terminal:

$ easy_install pdfslice

If the easy_install command is not available to you and you want to install it before installing pdfslice, you might want to go to the Easy Install homepage and follow the instructions there.

2. Manual installation

Alternatively, you can install the pdfslice tarball after downloading the file pdfslice-0.4.0.tar.gz and decompressing it with the following command:

$ tar xfz pdfslice-0.4.0.tar.gz

Then change into the newly created directory pdfslice and install pdfslice by running the following command:

$ python setup.py install

This will install a Python module file named pdfslice.py in the site-packages subfolder of your Python interpreter and a script tool named pdfslice in your bin directory, usually in /usr/local/bin.

Dependencies

Pdfslice depends on pyPdf which, if missing, will miraculously be installed together with pdfslice if you have a working internet connection during the installation procedure. If for whatever reason pyPdf cannot be installed, pdfslice should still install fine. In this case you'll get a warning when trying to run pdfslice.

Testing

The pdfslice tarball distribution contains a Unittest test suite in the file test_pdfslice.py which can be run like shown in the following lines on the system command-line:

$ tar xfz pdfslice-0.4.0.tar.gz
$ cd pdfslice
$ python test_pdfslice.py
written: samples/test-sliced.pdf
.written: samples/test-sliced-0.pdf
.written: samples/test-sliced-0..5.pdf
.written: samples/test-odd.pdf
.written: samples/test-reversed.pdf
....
----------------------------------------------------------------------
Ran 8 tests in 0.498s

OK

Bug reports

Please report bugs and patches to Dinu Gherman <gherman@darwin.in-berlin.de>. Don't forget to include information about the operating system and Python versions being used.