0

I am trying to export all active layers in a QGIS project to a specified folder, below is what I get atm

Outputdir='C:/Users/DN/Downloads/Test/'


layer = iface.activeLayer()

QgsVectorFileWriter.writeAsVectorFormat( layer, Outputdir + layer.name() + ".shp", "utf-8", layer.crs(), "ESRI Shapefile", 1)

It only works when I click on the layer that I want to export, I am wondering if it's possible to write a loop to loop through all active layers in the project and export them.

2

1 Answer 1

3

To iterate over all layers in the project and export them to a specified folder do the following:

We need identify all layers in the project using iface.mapCanvas().layers(). Then we can loop over the layers and export them.
Outputdir is the name of the folder where you would like to save to.

Outputdir = 'C:/temp/'

layers = iface.mapCanvas().layers()
for layer in layers:
    layerType = layer.type()
    if layerType == QgsMapLayer.VectorLayer:
        print (layer.name())
        QgsVectorFileWriter.writeAsVectorFormat( layer, Outputdir + layer.name() + ".shp", "utf-8", layer.crs(), "ESRI Shapefile" )

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