3

I've used the arcpy.mapping module for some time now to create mapbooks and other pdf documents, using a combination of Data Driven Pages and Python. I would like to take an existing pdf and save each individual page as separate pdfs.

I have read through the online documentation for PDFDocument, and while I can see the usefulness of the pageCount property, I don't see how I can access an individual page. The only reference I see to a page number is when using the insertPages method to insert a page before an existing page.

I know that using something like pypdf enables one to iterate over the pages in a pdf and other useful things, but I'd like to know if I can do this using arcpy before downloading it.

2
  • 1
    It could be messy but if you can get a PDFDocument object, and the number of pages in it then you should be able to open a copy of that PDF file and delete all but one page using deletePages (page_range) once either side of the page you are interested in.
    – PolyGeo
    Commented Aug 29, 2013 at 9:54
  • 1
    That would work but I agree that it would get messy. I just tried it with PyPDF2 and it worked brilliantly with little fuss, so I'll just integrate that into my arcpy script. Thanks for your input. Commented Aug 29, 2013 at 10:33

2 Answers 2

0

It will need a little code but with arcpy.mapping you can get a PDFDocument object, and the number of pages in it (pageCount property).

Consequently, you should be able to open a copy of that PDF file and delete all but one page using deletePages (page_range) with a range that can be specified either side of the page you are interested in.

Some code that I just wrote and tested successfully in ArcGIS 10.4.1 (I think it would have worked since 10.0) is below:

import arcpy

pdfPath = r"C:\Temp\test.pdf"
pdfDoc = arcpy.mapping.PDFDocumentOpen(pdfPath)
pageCount = pdfDoc.pageCount
pageList = []
for count in range(1,pageCount + 1):
    arcpy.Copy_management(pdfPath,pdfPath.replace("test","page"+str(count)))
    pageList.append(str(count))
del pdfDoc

print pageList

for count in range(1,pageCount + 1):
    print "Processing PDF for page {0} ...".format(count)
    pageDeleteList = list(pageList)
    pageDeleteList.remove(str(count))
    pages = ",".join(pageDeleteList)
    pdfDoc = arcpy.mapping.PDFDocumentOpen(pdfPath.replace("test","page"+str(count)))
    print "Deleting pages {0} ...".format(pages)
    pdfDoc.deletePages(pages)
    pdfDoc.saveAndClose()
    del pdfDoc

This will enable you to keep your installs and imports as simple as possible.

1
  • Yes, while it would be possible in arcpy, it certainly isn't the best way to do it, especially for something which is a non-GIS operation. I originally asked this question because I need to create new, individual mapbooks out of separated pages from an existing pdf document containing tables, along with their matching maps. Commented Oct 17, 2016 at 6:10
-1

As of 10.3.1, it is still not possible to do this using the arcpy.mapping module. Using other conventional Python libraries such as PyPDF2 seems to be the solution for now.

from PyPDF2 import PdfFileReader, PdfFileWriter
in_pdf = PdfFileReader(open(r'C:\Myfile.pdf', 'rb'))

for i in range(0, in_pdf.getNumPages()):
    output = PdfFileWriter()
    output.addPage(in_pdf.getPage(i))
    output_stream = file(r'C:\Myfile_' + str(i+1) + '.pdf', 'wb')
    output.write(output_stream)
1
  • I think it is incorrect to say that you cannot do this with arcpy.mapping - see my answer to this question.
    – PolyGeo
    Commented Oct 14, 2016 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.