2016-06-08 61 views
0

我有一個腳本,用於在導出數據驅動頁面時將插頁地圖放置到特定頁面上,該腳本是朋友工作和我自己的一些代碼的合併其他的項目。「if」語句在使用數據驅動頁面時循環遍歷兩次

問題是代碼導出頁面15和16兩次與我的插圖地圖和其他沒有,我不明白爲什麼。

我認爲這是與循環內的縮進,但我不能得到它,所以它的行爲以任何其他方式。任何幫助,將不勝感激!

import arcpy, os, time, datetime 
from datetime import datetime 
start_time = datetime.now() 
PageNumber = "Page " 

# Create an output directory variable i.e the location of your maps folder 
outDir = r"C:\Users\support\Desktop\Python\Book of Reference" 

# Create a new, empty pdf document in the specified output directory 
# This will be your final product 
finalpdf_filename = outDir + r"\FinalMapBook.pdf" 
if os.path.exists(finalpdf_filename): # Check to see if file already exists, delete if it does 
    os.remove(finalpdf_filename) 
finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename) 


# Create a Data Driven Pages object from the mxd you wish to export 
mxdPath = r"C:\Users\support\Desktop\Python\Book Of Reference\Book_Of_Reference_20160526_Python_Test.mxd" 
tempMap = arcpy.mapping.MapDocument(mxdPath) 
tempDDP = tempMap.dataDrivenPages 

# Create objects for the layout elements that will be moving, e.g., inset data frame, scale text 
Page15 = arcpy.mapping.ListDataFrames(tempMap)[1] 
Page16 = arcpy.mapping.ListDataFrames(tempMap)[2] 

# Instead of exporting all pages at once, you will need to use a loop to export one at a time 
# This allows you to check each index and execute code to add inset maps to the correct pages 

for pgIndex in range(1, tempDDP.pageCount + 1, 1): 

    # Create a name for the pdf file you will create for each page 
    temp_filename = r"C:\Users\support\Desktop\Python\Book of Reference\Book of Reference" + \ 
          str(pgIndex) + ".pdf" 
    if os.path.exists(temp_filename): 
    os.remove(temp_filename) #Removes pdf if it is already in the folder 

    # Code for setting up the inset map on the first page # 
    if (pgIndex == 15): 
    # Set position of inset map to place it on the page layout 
    Page15.elementPositionX = 20.1717 
    Page15.elementPositionY = 2.0382 
     # Set the desired size of the inset map for this page 
    Page15.elementHeight = 9.7337 
    Page15.elementWidth = 12.7115 
     # Set the desired extent for the inset map 
    Page15insetExtent = arcpy.Extent(518878,108329,519831,107599)  
    Page15insetExtent = Page15insetExtent 
    arcpy.RefreshActiveView() 
    tempDDP.exportToPDF(temp_filename, "RANGE", pgIndex) 
    finalPdf.appendPages(temp_filename) 
    Page15.elementPositionX = 50 #Move the Inset back off the page 
    arcpy.RefreshActiveView() #Refresh to ensure the Inset has been removed 
    print PageNumber + str(pgIndex) 


    if (pgIndex == 16): 
     # Set up inset map 
    Page16.elementPositionX = 2.1013 
    Page16.elementPositionY = 18.1914 
    Page16.elementHeight = 9.7337 
    Page16.elementWidth = 12.7115 
    Page16insetExtent = arcpy.Extent(520012, 107962, 521156,107086) 
    Page16insetExtent = Page16insetExtent 
    arcpy.RefreshActiveView() 
    print PageNumber + str(pgIndex) 
    tempDDP.exportToPDF(temp_filename, "RANGE", pgIndex) 
    finalPdf.appendPages(temp_filename) 
    print PageNumber + str(pgIndex) 
    Page16.elementPositionX = 50 
    arcpy.RefreshActiveView() 



    # Else Fuction takes care of the pages that dont have insets and just itterates through using the loop on line 28 
    else : 
    tempDDP.exportToPDF(temp_filename, "RANGE", pgIndex) 
    finalPdf.appendPages(temp_filename) 
    print PageNumber + str(pgIndex) 

# Clean up 
del tempMap 

# Update the properties of the final pdf 
finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS", 
          pdf_layout="SINGLE_PAGE") 
# Save your result 
finalPdf.saveAndClose() 

end_time = datetime.now() 
print('Duration: {}'.format(end_time - start_time)) 
+0

如果您檢查[網站](https://desktop.arcgis.com/en/arcmap/10.3/map/page-layouts/creating-a-map-book -with-inset-maps.htm)從您的代碼複製的位置開始,您會看到縮進是不同的。嘗試在標題爲「編寫導出腳本」下的示例中,通過從'if'語句中移動諸如'arcpy.RefreshActiveView()','tempDDP.exportToPDF(...)'等的行來匹配縮進。 – jDo

+1

移動線條工作,它現在運行完美。謝謝。 – GeordiePaddy

回答

1

我相信你的問題是,當pgIndex是15時,它按預期執行導出。然後它檢查pgIndex是否爲16. pgIndex不是16,因此它將落入else,並在沒有嵌入地圖的情況下重新導出。我會建議更改第二個如果對elif