Is there a way to merge all the layers with same name using python script in QGIS in the following list;

names = [layer.name() for layer in QgsMapLayerRegistry.instance().mapLayers().values()]
print names


[u'Pois', u'Pois', u'Pois', u'Pois', u'Pois', u'Pois', u'Pois', u'Pois', u'Pois']

So that when I list my current mapLayers, all I get is one layer with all the other layers merged into it.

3 Answers 3


You can use processing algorithms :

import processing

pois_lyr_list = []
lyr_list = QgsMapLayerRegistry.instance().mapLayers()
for layer in lyr_list:
    if layer.name() == u'Pois':

processing.runandload("qgis:mergevectorlayers", pois_lyr_list, "memory:merged")
  • I think @raosaeedali wants to merge layers which have the same name :)
    – Joseph
    Feb 7, 2018 at 12:00
  • @Joseph exactly. Feb 7, 2018 at 12:06
  • @joseph, Ok I added a loop to add in list only layers with name == u'Pois'
    – SYG
    Feb 7, 2018 at 12:07
  • Getting the following error; AttributeError: 'unicode' object has no attribute 'name' Feb 7, 2018 at 12:17
  • Figured it out. It was because in lyr_list u'Pois20180207171404510 was shown instead of u'Pois'. Feb 7, 2018 at 12:21

Here is an updated version for QGIS3. Note that I hardcoded CRS and result directory.

To get help for other CRS options you can run in Python Console:

import processing

Merge layers with same name script:

import processing

grouped_layers_by_name = {}
distinct_layer_names = set()

project_layers = QgsProject.instance().layerTreeRoot().children()

for layer_item in project_layers:
    layer_name = layer_item.name()
    grouped_layers_by_name[layer_name] = QgsProject.instance().mapLayersByName(layer_name);

print('Total number of distinct layers: ' + str(len(distinct_layer_names)))

for layer_name in distinct_layer_names:
    print('Processing layer: ' + layer_name + '... ', end='')
    merged_layer_file_name = 'D:/merged/' + layer_name + '.gpkg'
    alg_params = {
        'LAYERS': grouped_layers_by_name[layer_name],
        'CRS': 'EPSG:3765',
        'OUTPUT': merged_layer_file_name

    result = processing.run('native:mergevectorlayers', alg_params)

print('All layers have been merged.')
  • Can this be use without, because I am looking for something similar but I have tables without geometry and I want to export it as csv/xls. Aug 6, 2020 at 14:36

Simple version updated for QGIS3

import processing

listLayers = QgsProject.instance().mapLayersByName('Points')


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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