对于我的工作,我必须编写一个python脚本来生成几个地图,显示不同学校的类似数据。我正在使用光标循环浏览我的列表,这实际上对我列表中的前28个学校来说非常有效。但是,在将下一个映射导出到pdf文件时,我收到一条错误消息。
AttributeError:PageLayoutObject:执行ExportToPDF时出错
我还尝试将地图仅导出到jpeg文件,该文件的工作时间稍长,但也会在ca.具有相同错误的50次迭代。
我不知道为什么我的脚本在前28次迭代中起作用然后停止。有没有人有想法?可能是Python无法将这么多文件保存到我指示的文件夹中吗?或者内存是填满还是什么?我该怎么办呢?
我对Python的一般编程很新,所以我不知道如何解决这个问题。
以下是我的代码的一部分(可能会有所帮助):
首先启动Cursor:
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]))
Loop的总代码:
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 :(得分:0)
尝试摆弄你的循环从第28张地图获得的变量。看起来你正在使用csv文件,可能会将工作地图的列复制到不起作用的行,只是为了确认它是否是内存问题。一旦你确认三重检查你的第28所学校的所有价值。在尝试转换为pdf时,看起来其中一个可能无效。