3

I am trying to add description to layer and then make a layer package of multiple files at once. Everything works just fine, but the formatting of description is just wrong.

I want to add new lines after each line. What i tried is adding \n, \r \n, '\n', '\r \n', \u000d,
.

None of these are working.I simply ran out of ideas. In help there is nothing about the formatting.

files = arcpy.ListRasters()

name = "Hazard Index"

description = "Hazard Index  \
   \
   Ranges of depths for different level of hazard \
   \
   Depth (d)\
   \
   not flooded     No Hazard    0"                                             


for file in files:
   raster_layer = arcpy.MakeRasterLayer_management (file, file[:-4])

   #raster_layer.name = file[:-4]
   arcpy.ApplySymbologyFromLayer_management(raster_layer, layer)

   lyr = arcpy.SaveToLayerFile_management (raster_layer, output + file[:-4] + ".lyr")

   raster_lyr = arcpy.mapping.Layer(str(lyr))

   raster_lyr.description = description
   raster_lyr.name = file[:-4]

   arcpy.PackageLayer_management(raster_lyr, output + file[:-4], "PRESERVE", "", "", "", "", "", "", summary, tags)
   print "Package created for: " + file
   os.remove(str(lyr))

result is (I did not copy all of the text here, but you get the idea)

result

4

Sometimes the best way to find how to do something is to see how ArcMap does it.

I added a raster layer to my map, and set a Description in the layer properties

enter image description here

I then used arcpy to show me that description (using ArcMap's built-in Python window). Note I did not use print, as that won't show the escape characters.

>>> raster_lyr.description
u'This is a\r\nraster layer\r\ndescription'

As you can see from the result, to force a new line in the layer description you need to use a combination of \r and \n

Therefore, in your code to force those new lines you can enter the following:

description = "Hazard Index\r\n\r\nRanges of depths for different level of hazard \r\n\r\nDepth (d)\r\n\r\nnot flooded No Hazard 0"

raster_lyr.description = description

enter image description here

| improve this answer | |
  • Well, I guess I just put a space between \r\n, because I used something similar. Thank you very much. – Jan Doležal Dec 22 '17 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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