How to delete pages from pdf file using Python?
<p>I have some .pdf files with more than 500 pages, but I need only a few pages in each file. It is necessary to preserve document`s title pages. I know exactly the numbers of the pages that program should remove. How I can do it using Python 2.7 Environment, which is installed upon MS Visual Studio?</p>

<p>Try using <a href="https://pypi.python.org/pypi/PyPDF2/1.26.0" rel="nofollow">PyPDF2</a>. Some sample code (adapted from <a href="https://www.binpress.com/tutorial/manipulating-pdfs-with-python/167" rel="nofollow">here</a>).</p>

<pre><code>pages_to_keep = [1,2,10]
from PyPDF2 import PdfFileWriter, PdfFileReader
infile = PdfFileReader('source.pdf', 'rb')
output = PdfFileWriter()

for i in xrange(infile.getNumPages()):
    p = infile.getPage(i)
    if i in pages_to_keep: # getContents is None if  page is blank
        output.addPage(p)

with open('newfile.pdf', 'wb') as f:
   output.write(f)
</code></pre>

python pdf