Use Page Ranges in PyPDF2 PdfFileMerger

PdfFileMerger is a nice Python class provided by the PyPDF package. If you want to catenate multiple pages using expressions you can use a Page Range expression.

Remember to import the PageRange class

from PyPDF2 import PdfFileMerger, PageRange

then you can specify the range as a PageRange() object. For example, the range from page 131 (REMEMBER: page indices start with zero.!) to the last one:

merger.append(pdf, pages=PageRange('130:-1'))

More page range expression examples follows, if you want to play a bit with them:

  :     all pages.                   -1    last page.
  22    just the 23rd page.          :-1   all but the last page.
  0:3   the first three pages.       -2    second-to-last page.
  :3    the first three pages.       -2:   last two pages.
  5:    from the sixth page onward.  -3:-1 third & second to last.

The third, “stride” or “step” number is also recognized.

  ::2       0 2 4 ... to the end.    3:0:-1    3 2 1 but not 0.
  1:10:2    1 3 5 7 9                2::-1     2 1 0.
  ::-1      all pages in reverse order.
notes/use_page_ranges_in_pypdf2_pdffilemerger.txt · Last modified: 2017/11/02 17:22 by admin
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Run by Debian Driven by DokuWiki