Skip to content

Instantly share code, notes, and snippets.

@franquis
Created October 3, 2016 13:02
Show Gist options
  • Save franquis/9fcfa3676a53ddfc698005d8f93e0d82 to your computer and use it in GitHub Desktop.
Save franquis/9fcfa3676a53ddfc698005d8f93e0d82 to your computer and use it in GitHub Desktop.
Reverse page order in PDF file with Python
#!/usr/bin/python
"""
This script reverse the pages of a PDF document and save it to a new file.
Dependancy :
- "PDFRW"
Usage :
- `pip install pdfrw`
- `python reverse.py file.pdf`
"""
from pdfrw import PdfReader, PdfWriter
import sys
import os
def reverse(pdf_file):
writ = PdfWriter()
read = PdfReader(pdf_file)
for page in reversed(read.pages):
writ.addpage(page)
writ.write('reversed_%s' % pdf_file)
if __name__ == "__main__":
if(len(sys.argv) == 1):
print("Usage `python reverse.py mypdffile.pdf`")
exit(0)
pdf = sys.argv[1]
if os.path.isfile(pdf):
reverse(pdf)
print("Reversed file '%s'" % pdf)
else:
print("File not found")
@hamaney
Copy link

hamaney commented May 22, 2017

pyhton3 reverse.py 1.pdf

output:
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20755, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20757, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20758, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20753, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20760, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20754, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20759, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20764, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20765, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20756, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20761, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20763, 0)
[ERROR] uncompress.py:72 Error -3 while decompressing data: incorrect header check (20762, 0)
[WARNING] tokens.py:228 Did not find PDF object (2319, 0) (line=179958, col=1, token='endobj')
[ERROR] pdfreader.py:472 Invalid page tree: 'NoneType' object is not subscriptable

note: used it on a scanned pdf.
any idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment