Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rearranging Pages of a PDF

Jorj X. McKie edited this page Sep 28, 2019 · 6 revisions

Using Document.select()

Since V 1.9.0, the Document class has a new method, select(s). The only parameter is a sequence s of pages (given by zero-based integers), that should be left over ("selected").

As is mentioned in the manual, s may be a Python list, tuple, array.array, numpy.array, or any other object that implements the sequence protocol and contains integers.

Successful execution will alter the document's representation in memory.

For example, after select([0]), only the first page will be left over, everything else will have gone, pageCount will be 1, and so on. If you now save the document by save(...), you will have a new 1-page PDF reflecting what has happened.

Interesting to note, that all links, bookmarks and annotations will be preserved, if they do not point to deleted pages.

How can this method be used?

If you know how to manipulate Python lists, you are only limited by your imagination. For example

  • Delete pages containing no text or a specific text
  • Only include odd / even pages, e.g. to support double sided printing on some printer hardware
  • Re-arrange pages, e.g. the whole document from back to front: take lst = list(range(doc.pageCount-1, -1, -1)) as the list to be selected.
  • "Concatenate" a document with itself by specifying lst + lst as the list of pages to be taken
  • doc.select([1,1,1,5,5,5,9,9,9]) does what it looks like: create a 9-page document of 3 times 3 equal pages
  • Take the first / last 10 pages: lst = list(range(10)), lst = list(range(doc.pageCount - 10, doc.pageCount)), respectively.
  • etc.

You can apply several such selects in a row. After each one, the document structure will get updated (len(doc)will always reflect the current page count, etc.).

The original PDF content is no longer accessible. But you can discard changes: close and re-open the document.

Save your work using doc.save(...). Be sure to include the garbage=4 option if you have deleted many pages and want to regain disk space.

Using Other Methods

When select() is a caliber too big to achieve something simple: Consider using Documentmethods deletePage(), deletePageRange(), copyPage() or movePage(). These methods offer a more direct, intuitive approach when only few pages are concerned. They are technically implemented in a different, more efficient way.

Recipes

HOWTO Button annots with JavaScript

HOWTO extract images

HOWTO join PDFs

HOWTO work with PDF embedded files

HOWTO Convert Images

HOWTO extract text from inside rectangles

HOWTO extract text in natural reading order

HOWTO add PDF form fields

HOWTO deal with annotations

HOWTO convert to PDF

HOWTO show PDF Form fields

HOWTO work with vector images

HOWTO create or extract graphics

HOWTO create your own PDF Drawing

HOWTO add pages, images, text

HOWTO extract fonts

HOWTO rearrange pages

HOWTO GUI PDF display

Algebra with geometry objects

Rectangle inclusion & intersection

Hyperlink maintenance

Visual table extraction

Incremental saves

Metadata & bookmark maintenance

Wrapping FileOptimizer

Installation

Ubuntu

Ubuntu Installation Experience

Windows Binaries

Windows Binaries Generation

Windows Binaries Installation

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /