How to extract some PDF pages from a PDF file and save to a new PDF file using Python?

VeryUtils has a Python-related PDF library, PythonPDF Library, you can use this Python PDF Library to execute splitting, merging, rotation, u-up pages, watermarking and stamping options to your PDF files easily. PythonPDF Library is a pure Python PDF Library, it's a standalone library, and without depend on any other third party applications.

image

PythonPDF Library can be purchased from this web page,

https://veryutils.com/pythonpdf-library-source-code

after you purchase it online, you will get a download URL for the latest version, please download and unzip it to a folder, then you can use it without any restrictions.

Please by following steps to use VeryUtils PythonPDF Library to extract some PDF pages from an existing PDF file and save them to a new PDF file,

1. Please unzip PythonPDF Library to a folder, such as D:\downloads\python-pdfrw folder,

2. Please run following command line to set the main folder to "PYTHONPATH" environment variable,

set PYTHONPATH=D:\downloads\python-pdfrw

3. Please go to "examples" folder, you can run following command line to extract pages "1-3 5 7-9" from testcmd.pdf file and save them to a new PDF file,

python subset.py testcmd.pdf 1-3 5 7-9

subset.py is a simple python script, you can run it in Command Line window to extract some pages from a PDF file and save to a new PDF file,
------------------------
usage:   subset.py my.pdf page[range] [page[range]] ...
         eg. subset.py 1-3 5 7-9

Creates subset.my.pdf
------------------------
The following is the source code of subset.py file,
------------------------
import sys
import os

from pdfrw import PdfReader, PdfWriter

inpfn = sys.argv[1]
ranges = sys.argv[2:]
assert ranges, "Expected at least one range"

ranges = ([int(y) for y in x.split('-')] for x in ranges)
outfn = 'subset.%s' % os.path.basename(inpfn)
pages = PdfReader(inpfn).pages
outdata = PdfWriter(outfn)

for onerange in ranges:
    onerange = (onerange + onerange[-1:])[:2]
    for pagenum in range(onerange[0], onerange[1]+1):
        outdata.addpage(pages[pagenum-1])
outdata.write()
------------------------

If you have any question for "PythonPDF Library", please feel free to let us know, we are glad to assist you asap.

VN:F [1.9.20_1166]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.20_1166]
Rating: 0 (from 0 votes)

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *


Verify Code   If you cannot see the CheckCode image,please refresh the page again!