Is it possible to add multiple/all layers from a WMS service with PyQGIS?

It is possible to add a WMS layer with PyQGIS if layer name to be added is exactly known. (which is 'MA-ALUS' in the code below)

urlWithParams = 'url=http://kaart.maaamet.ee/wms/alus&format=image/png&layers=MA-ALUS&styles=&crs=EPSG:3301' 
rlayer = QgsRasterLayer(urlWithParams, 'MA-ALUS', 'wms') 

Assuming that if want to add all of the layers in the WMS service, what should be the proper PyQGIS code/implementation?

e.g. code below gives a list of all layers in the WMS if it is executed under QGIS Python Console (so that one can choose and add multiple layers).


My question is; is it possible to implement similar operation with PyQGIS?

2 Answers 2


The simplest way to do this, is to set the layername=0. In most cases even in both of your examples it works.

Try url=http://kaart.maaamet.ee/wms/alus&format=image/png&layers=0&styles=&crs=EPSG:3301

  • Already tried that but it does not work. WMS URL gis-prod-api.csb.gov.tr/orman_wms has many layers in. Service works. You can add desired layer via PyQGIS by specifying desired layer name e.g. layer "18". But if you try to add layer "0" you get following message "Oop! test_wms cannot be loaded. Either the server is down or you have limited internet connectivity" Any suggestion?
    – cempro
    Apr 26, 2017 at 11:19
  • Sorry, couldn't test this, wms doesn't work for me, because server was not found. Apr 26, 2017 at 11:56

The post is old but I have the answer :

In your exemple :


You must have same number of "layers" and "styles" attributes.

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.