2015-12-03 95 views
0

對於我的工作,我必須編寫一個python腳本來生成幾張地圖,以顯示不同學校的相似數據。我使用一個光標循環訪問我的列表,實際上我的列表中前28所學校工作得很好。但是,當我將下一個導圖導出爲PDF文件時,我收到了一條錯誤消息。Python在一些迭代後停止循環遍歷列表

AttributeError: PageLayoutObject: Error in executing ExportToPDF

我也嘗試導出地圖只到一個jpeg文件,它的工作時間稍長一些,但也在ca之後停止。 50次迭代有相同的錯誤。

我不知道爲什麼我的腳本爲前28次迭代工作,然後停止。有人有想法嗎?可能是因爲Python無法將這麼多文件保存到我正在指向的文件夾中?或者是記憶填滿了什麼?我能做些什麼呢?

我很新的Python在一般的編程,所以我不知道如何解決這個問題。

這裏是(這可能會幫助)我的代碼部分:

與initilizing光標開頭:

Schulliste = "filepath/Schulliste_PrSt.csv" 
fields = ('Schule_Nr', 'Schule_Name') 
CursorSchule = arcpy.da.SearchCursor(Schulliste, fields) 
for row in CursorSchule: 
*[a lot of tasks to set up the map]* 

代碼的最後保存地圖PDF和JPEG文件:

# Karte exportieren 
if SF == "STS": 
    arcpy.mapping.ExportToPDF(mapdoc, "filepath/Einzugsgebiet_{0}.pdf".format(row[0])) 
    arcpy.mapping.ExportToJPEG(mapdoc, "filepath/Einzugsgebiet_{0}.jpg".format(row[0])) 
elif SF == "G": 
    arcpy.mapping.ExportToPDF(mapdoc, "filepath/Einzugsgebiet_{0}.pdf".format(row[0])) 
    arcpy.mapping.ExportToJPEG(mapdoc, "filepath/Einzugsgebiet_{0}.jpg".format(row[0])) 

for循環的總代碼:

for row in CursorSchule: 
    #define mapdoc and dataframes 
    mxd = "filepath/Standard_Einzug.mxd" 
    mapdoc = arcpy.mapping.MapDocument(mxd) 
    df = arcpy.mapping.ListDataFrames(mapdoc)[0] 
    df_Sus = arcpy.mapping.ListDataFrames(mapdoc)[1] 


    #define new layers 
    lyrSchule = arcpy.mapping.Layer("filepath/FHH_Schulen_SJST_2014_SingleSym_red.lyr") 
    lyrNachbarsch = arcpy.mapping.Layer("filepath/FHH_Schulen_SJST_2014_SingleSym_blue.lyr") 
    lyrEinzug = arcpy.mapping.Layer("filepath/FHH_StatGeb_2011_GradCol.lyr") 

    #define legends 
    legend1 = arcpy.mapping.ListLayoutElements(mapdoc, "LEGEND_ELEMENT", "Legende1")[0] 
    legend2 = arcpy.mapping.ListLayoutElements(mapdoc, "LEGEND_ELEMENT", "Legende2")[0] 

    #add table for every School with table join 
    folder = "filepath/" 
    joinTable = join(folder, "Daten_{0}.csv".format(row[0])) 
    arcpy.AddJoin_management(lyrEinzug, "StatGeb_Nr", joinTable, "StatGeb_Nr") 
    arcpy.FeatureClassToShapefile_conversion(lyrEinzug, "filepath") 

    #add saved shapefile 
    lyrEinzug = arcpy.mapping.Layer("GPL0.shp") 
    legend1.autoAdd = False 
    legend2.autoAdd = True 
    arcpy.mapping.AddLayer(df, lyrEinzug) 
    for lyr in arcpy.mapping.ListLayers(mapdoc): 
     if lyr.name == "GPL0": 
      lyr.name = "Gebietsbezogen" 

    #Change symbology of the layer to graduated Color 
    lyrEinzug = arcpy.mapping.ListLayers(mapdoc, "Gebietsbezogen")[0] 
    sourceLayer = arcpy.mapping.Layer("filepath/StatGeb_Label_Halo_Symbol.lyr") 
    arcpy.mapping.UpdateLayer(df, lyrEinzug, sourceLayer, False) 
    arcpy.RefreshActiveView() 
    lyrEinzug = arcpy.mapping.ListLayers(mapdoc, "Gebietsbezogen")[0] 

    #Change symbology classes 
    fieldlist = arcpy.ListFields("filepath/GPL0.shp") 
    valueField = fieldlist[-1] 
    lyrEinzug.symbology.valueField = valueField.name 
    lyrEinzug.symbology.classBreakValues = [0, 5, 10, 20, 40, 70, 100] 
    lyrEinzug.symbology.classBreakLabels = ["unter 5% der SuS", "5 bis unter 10% der SuS", "10 bis unter 20% der SuS", "20 bis unter 40% der SuS", "40 bis unter 70% der SuS", "über 70% der SuS"] 
    lyrEinzug.transparency = 50 

    #Data Frame Extent 
    queryField = fieldlist[7] 
    fieldname = queryField.name 
    delimfield = arcpy.AddFieldDelimiters(lyrEinzug, fieldname) 
    attribute_query = delimfield + " = {0}".format(row[0]) 
    lyrEinzug.definitionQuery = attribute_query 
    df.extent = lyrEinzug.getSelectedExtent(False) 
    arcpy.RefreshActiveView() 

    #add labels 
    labelField = fieldlist[9] 
    if lyrEinzug.supports("LABELCLASSES"): 
     for lblclass in lyrEinzug.labelClasses: 
      lblclass.showClassLabels = True 
    lblclass.expression = "\"<FNT name='Arial' size='3'><CLR green = '112' blue = '255'>\"" + " & [" + labelField.name + "] & " + '"</CLR></FNT>"' 
    lyrEinzug.showLabels = True 
    arcpy.RefreshActiveView() 

    #select specific school 
    fieldSchule = "Schule_Nr" 
    delimfieldSchule = arcpy.AddFieldDelimiters(lyrSchule, fieldSchule) 
    querySchule = delimfieldSchule + " = {0}".format(row[0]) 
    lyrSchule.definitionQuery = querySchule 
    arcpy.FeatureClassToShapefile_conversion(lyrSchule, "filepath/02_GIS-Daten") 
    lyrSchule = arcpy.mapping.Layer("GPL0_1.shp") 
    legend1.autoAdd = True 
    legend2.autoAdd = False 
    arcpy.mapping.AddLayer(df, lyrSchule, "TOP") 
    lyrSchule = arcpy.mapping.ListLayers(mapdoc, "GPL*")[0] 
    sourceLayer = arcpy.mapping.Layer("filepath/Schule_Labels_Halo.lyr") 
    arcpy.mapping.UpdateLayer(df, lyrSchule, sourceLayer, False) 
    lyrSchule.name = "{0} ({1})".format(row[1], row[0]) 

    if lyrSchule.supports("LABELCLASSES"): 
     for lblclass in lyrSchule.labelClasses: 
      lblclass.showClassLabels = True 
      lblclass.expression = '"%s" & [Schule_Nam] & "%s"' % ("<BOL><FNT name='Arial' size='3'><CLR red = '255'>", "</CLR></FNT></BOL>") 
    lyrSchule.showLabels = True 
    arcpy.RefreshActiveView() 

    #select neighboring Schools within 1.5 km 
    arcpy.Buffer_analysis("filepath/buffer", 1500) 
    lyrBuffer = arcpy.mapping.Layer("buffer.shp") 
    arcpy.SelectLayerByLocation_management(lyrNachbarsch, "WITHIN", lyrBuffer) 
    arcpy.FeatureClassToShapefile_conversion(lyrNachbarsch, "filepath") 
    lyrNachbarsch = arcpy.mapping.Layer("filepath/GPL0_2.shp") 
    legend1.autoAdd = True 
    legend2.autoAdd = False 
    arcpy.mapping.InsertLayer(df, lyrSchule, lyrNachbarsch, "AFTER") 
    for lyr in arcpy.mapping.ListLayers(mapdoc): 
     if lyr.name == "GPL0_2": 
      lyr.name = "Nachbarschulen (<1,5km)" 
    lyrNachbarsch = arcpy.mapping.ListLayers(mapdoc, "Nachbar*")[0] 
    sourceLayer = arcpy.mapping.Layer("filepath/FHH_Schulen_SJST_2014_SingleSym_blue.lyr") 
    arcpy.mapping.UpdateLayer(df, lyrNachbarsch, sourceLayer, True) 

    #if selected School is elementary School, also only select neighbouring elementary Schools 
    SC = arcpy.SearchCursor(lyrSchule) 
    for Schule in SC: 
     SF = Schule.getValue("Schule_SF") 
    fieldform = "Schule_SF" 
    delimfield1 = arcpy.AddFieldDelimiters(lyrNachbarsch, fieldform) 
    query_SF = delimfield1 + "= 'G'" 
    lyrNachbarsch.definitionQuery = query_SF 
    arcpy.RefreshActiveView() 

    del SC, Schule 

    #Labels for schools 
    fieldname = "Schule_Nr" 
    delimfield2 = arcpy.AddFieldDelimiters(lyrNachbarsch, fieldname) 
    query_Nachbar = delimfield2 + "<> {0}".format(row[0]) 
    if lyrNachbarsch.supports("LABELCLASSES"): 
     for lblclass in lyrNachbarsch.labelClasses: 
      lblclass.className = "Schulname" 
      lblclass.SQLQuery = query_Nachbar 
      lblclass.expression = '"%s" & [Schule_Nam] & "%s"' % ("<FNT name='Arial' size='3'><CLR red = '100' blue = '100' green = '100'>", "</CLR></FNT>") 
      lblclass.showClassLabels = True 
    lyrNachbarsch.showLabels = True 
    arcpy.RefreshActiveView() 

    #Change text 
    #Titel 
    Titel = arcpy.mapping.ListLayoutElements(mapdoc, "TEXT_ELEMENT", "Titel")[0] 
    Titel.text = "Einzugsgebiet Jahrgänge 1-4" 

    #Legende 
    style_StatGeb = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", "Einzug")[0] 
    style_Schulen = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", "Schule")[0] 
    legend1.updateItem(lyrSchule, style_Schulen) 
    legend1.updateItem(lyrNachbarsch, style_Schulen) 
    legend2.updateItem(lyrEinzug, style_StatGeb) 
    legend1.elementPositionY = 25 
    legend2.elementPositionY = 11.0 
    legend2.elementPositionX = 1.5 

    #number of pupils 
    lyrSchuldaten = arcpy.mapping.Layer("filepath/Schulliste_PrSt.lyr") 
    arcpy.mapping.AddLayer(df_Sus, lyrSchuldaten) 
    lyrSchuldaten = arcpy.mapping.ListLayers(mapdoc, "Schulliste*")[0] 
    querySchule = delimfield2 + "= {0}".format(row[0]) 
    lyrSchuldaten.definitionQuery = querySchule 
    df_Sus.extent = lyrSchuldaten.getSelectedExtent(False) 
    arcpy.RefreshActiveView() 

    #Infotext 
    Infotext = arcpy.mapping.ListLayoutElements(mapdoc, "TEXT_ELEMENT", "Infotext")[0] 
    Infotext.text = "nicht darstellbar: SuS aus dem Umland \n \nAggregation: Statistische Gebiete \nKlassenberechnung: manuell gesetzte Intervalle \nDatenbezug: Wohnort der SuS \nDatenauszug: 10.01.14 \nKartengrundlage: DISK60" 
    Infotext.elementPositionY = 1.73 

    #Export map 
    if SF == "STS": 
     arcpy.mapping.ExportToPDF(mapdoc, "filepath/Einzugsgebiet_{0}.pdf".format(row[0])) 
     arcpy.mapping.ExportToJPEG(mapdoc, "filepath/Einzugsgebiet_{0}.jpg".format(row[0])) 
    elif SF == "G": 
     arcpy.mapping.ExportToPDF(mapdoc, "filepath/Einzugsgebiet_{0}.pdf".format(row[0])) 
     arcpy.mapping.ExportToJPEG(mapdoc, "filepath/Einzugsgebiet_{0}.jpg".format(row[0])) 


    #delete shapefiles 
    arcpy.Delete_management("GPL0.shp") 
    arcpy.Delete_management("GPL0_1.shp") 
    arcpy.Delete_management("GPL0_2.shp") 

編輯:

我試圖JPEG文件保存具有更高的解決方案今天這導致了比以前早了很多錯誤信息可能會強調它有什麼做的可用內存?

+0

我不認爲它與Python有任何關係。請嘗試找出哪個文件正在返回錯誤,可能文件已損壞,或者內存不足。 Python停止循環,因爲它得到一個錯誤。 –

+0

感謝您的回答,那就是我期望的......我知道哪個文件正在返回錯誤,但與文件本身無關。僅爲該文件運行進程完全正常。我可能不能改變關於記憶的任何事情,對吧? – Elena

+0

單次運行很好,可能是很多事情。很難從這裏說。你確定你可以處理單個文件嗎?當你測試它時,你是否運行了與腳本完全相同的命令? –

回答

0

嘗試擺弄循環從第28張地圖中獲取的變量。它看起來像你正在使用一個csv文件的所有這一切,也許複製工作地圖的列到不工作的行,只是爲了確認它是否是一個內存問題。一旦你確認三重檢查你第28所學校的所有價值。它看起來像其中一個可能無效時,試圖轉換爲PDF格式。

+0

正如我之前說過的,當我只爲這所學校計算一切時,它就完全正常了。我再次嘗試,它與變量無關 – Elena