Select to view content in your preferred language

Copy Multiple PDF output pages to a new folder in python

2732
2
05-25-2012 03:58 AM
KevinParson
Deactivated User
I have to separate a large mapbook of individual pages (900+ pages) into separte folders but I need to retain the original files in the source folder.
There are 17 different folders. Also in another series I may have pages numbered 1, 14, 845.
Is there a better way instead of listing individual pages in the copy line for each folder?
Ideally I would like to do this by range so I can use it to another mapbook series that skips pages.
I am new to py scripting so any help is appreciated.


Many thanks, Kevin
#

import arcpy, os
from shutil import copy

# Code for mapbook in here

# Copy for Web A
inDir = r"C:\Work\City_Wide_Zoning\Mapbook\Mapbook\PDF\z_Grid\Test2"
outDir = r"C:\Work\City_Wide_Zoning\Mapbook\Mapbook\PDF\z_Grid\Test2\A"

copy(inDir + r"\1.pdf", outDir + r"\1.pdf")
copy(inDir + r"\14.pdf", outDir + r"\14.pdf")

# etc for Folder B, C, and so on
Tags (2)
0 Kudos
2 Replies
FabianBlau
Deactivated User
Maybe this could help you:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000001w000000

or this:
os.walk -> http://docs.python.org/library/os.html
os.path.walk -> http://docs.python.org/library/os.path.html

or something like this:
wild_card = "*" # for all or something like "*_pdfs" for special folders if they all subfolders in the same folder
inFolders = arcpy.ListWorkspaces (wild_card, "Folder")
# or:
inFolders = ['folder1', 'folder2'] # manual listing the directories
for inFolder in inFolders:
    files = arcpy.ListFiles("*.pdf")
    # print the filenames to check if this works. I am not sure if you get the filename with or without the complete path
    print files
    arcpy.AddMessage("%s" % files) 
    for file in files:
        # file = os.path.basename(file) # if listfiles returns the complete path
        copy(os.path.join(inFolder, file), os.path.join(outDir, file))
0 Kudos
KevinParson
Deactivated User
Thanks for your help Fabian. You pointed me in the right direction.

I have settled on something like this at the present:


# Copy for Web A

os.mkdir (r"C:\Work\City_Wide_Zoning\Mapbook\Mapbook\PDF\z_Grid\Test3\A")
os.chdir(r"C:\Work\City_Wide_Zoning\Mapbook\Mapbook\PDF\z_Grid\Test3")

for file in os.listdir('.'):

    if fnmatch.fnmatch(file, 'A*.pdf'):
        
        print file
        copy(file, r"C:\Work\City_Wide_Zoning\Mapbook\Mapbook\PDF\z_Grid\Test3\A/" + file)

print 'Folder A Complete'
0 Kudos