Lithimlin
Lithimlin

Reputation: 560

Switching the order of pages of a pdf in python

I'm currently working on some PDF file generation in python for nametags. However, in my freshly generated files I have all fronts and then all backs instead of a front, then the according back, then the next front and so on. I would like to correct that after the files have been generated.

So I have the following:

p1f, p2f, p3f,... ,p1b, p2b, p3b,...

Where pn describes the n-th page, f is for front and b is for back. What I want to end up with is:

p1f, p1b, p2f, p2b, p3f, p3b,...

What are possible ways to approach this? What libraries could I use?

Thanks in advance!

Upvotes: 1

Views: 1073

Answers (1)

Marco
Marco

Reputation: 579

For libraries you can use PyPDF2 or pdfrw.

For approaches I'd suggest when you have small files: load them into memory, reorder pages, and write them back to disk. If a PDF file is too large you could split pages into sperate files and build the output file one page after another. However it is safe to say that there are more efficient ways to do this.

Also you might want to check PDF-Shuffler which is a python-gtk tool to perform such tasks on a non programmatic basis.

Upvotes: 2

Related Questions