Skip to main content
1 of 3

How to batch label features using Python (Qgis)

I am writing a python script to label features (lines). However I can't figure out:

  1. how to get the script to turn on the buffer

  2. how to get the script to apply the changes. When I currently run it, it changes everything in the properties (except the buffer) but I still have to hit ok before QGIS applies it to the map.

.

layer = iface.activeLayer()
layer.setCustomProperty("labeling/fieldName", "Length" )
layer.setCustomProperty("labeling/placement", QgsPalLayerSettings.Horizontal)
layer.setCustomProperty("labeling/predefinedPointPosition", QgsPalLayerSettings.BottomRight)
layer.setCustomProperty("labeling/fontSize","10" )
layer.setCustomProperty("labeling/buffer",QgsPalLayerSettings.BufferDraw)
layer.setCustomProperty("labeling/enabled","true" )
layer.triggerRepaint()

I have had a look at one similar question but I cannot seem to figure it out: Labeling line features using PyQGIS?

Once I get this to work I want to transform it into a batch-script by adding some lines like:

for layer in QgsMapLayerRegistry.instance().mapLayers().values():