Skip to main content
Bounty Ended with 50 reputation awarded by CEL-ma
syntax-highlighting
Source Link
barteksch
  • 685
  • 4
  • 9
import os
from qgis.core import QgsMapLayerStyle
from qgis.utils import iface

# set path to your styles here
qml_path = '/home/user/qml'

layer = iface.activeLayer()
style_manager = layer.styleManager()

# read valid style from layer
style = QgsMapLayerStyle()
style.readFromLayer(layer)

for qml_file in [f for f in os.listdir(qml_path)
                 if os.path.isfile(os.path.join(qml_path, f)) and
                 f.endswith('.qml')]:
    # get style name from file
    style_name = os.path.basename(qml_file).strip('.qml')
    # add style with new name
    style_manager.addStyle(style_name, style)
    # set new style as current
    style_manager.setCurrentStyle(style_name)
    # load qml to current style
    (message, success) = layer.loadNamedStyle(os.path.join(qml_path, qml_file))
    print message
    if not success:  # if style not loaded remove it
        style_manager.removeStyle(style_name)
import os
from qgis.core import QgsMapLayerStyle
from qgis.utils import iface

# set path to your styles here
qml_path = '/home/user/qml'

layer = iface.activeLayer()
style_manager = layer.styleManager()

# read valid style from layer
style = QgsMapLayerStyle()
style.readFromLayer(layer)

for qml_file in [f for f in os.listdir(qml_path)
                 if os.path.isfile(os.path.join(qml_path, f)) and
                 f.endswith('.qml')]:
    # get style name from file
    style_name = os.path.basename(qml_file).strip('.qml')
    # add style with new name
    style_manager.addStyle(style_name, style)
    # set new style as current
    style_manager.setCurrentStyle(style_name)
    # load qml to current style
    (message, success) = layer.loadNamedStyle(os.path.join(qml_path, qml_file))
    print message
    if not success:  # if style not loaded remove it
        style_manager.removeStyle(style_name)
import os
from qgis.core import QgsMapLayerStyle
from qgis.utils import iface

# set path to your styles here
qml_path = '/home/user/qml'

layer = iface.activeLayer()
style_manager = layer.styleManager()

# read valid style from layer
style = QgsMapLayerStyle()
style.readFromLayer(layer)

for qml_file in [f for f in os.listdir(qml_path)
                 if os.path.isfile(os.path.join(qml_path, f)) and
                 f.endswith('.qml')]:
    # get style name from file
    style_name = os.path.basename(qml_file).strip('.qml')
    # add style with new name
    style_manager.addStyle(style_name, style)
    # set new style as current
    style_manager.setCurrentStyle(style_name)
    # load qml to current style
    (message, success) = layer.loadNamedStyle(os.path.join(qml_path, qml_file))
    print message
    if not success:  # if style not loaded remove it
        style_manager.removeStyle(style_name)
import os
from qgis.core import QgsMapLayerStyle
from qgis.utils import iface

# set path to your styles here
qml_path = '/home/user/qml'

layer = iface.activeLayer()
style_manager = layer.styleManager()

# read valid style from layer
style = QgsMapLayerStyle()
style.readFromLayer(layer)

for qml_file in [f for f in os.listdir(qml_path)
                 if os.path.isfile(os.path.join(qml_path, f)) and
                 f.endswith('.qml')]:
    # get style name from file
    style_name = os.path.basename(qml_file).strip('.qml')
    # add style with new name
    style_manager.addStyle(style_name, style)
    # set new style as current
    style_manager.setCurrentStyle(style_name)
    # load qml to current style
    (message, success) = layer.loadNamedStyle(os.path.join(qml_path, qml_file))
    print message
    if not success:  # if style not loaded remove it
        style_manager.removeStyle(style_name)
Source Link
barteksch
  • 685
  • 4
  • 9

You can load multiple styles using pyqgis script (explanations in comments):

import os
from qgis.core import QgsMapLayerStyle
from qgis.utils import iface

# set path to your styles here
qml_path = '/home/user/qml'

layer = iface.activeLayer()
style_manager = layer.styleManager()

# read valid style from layer
style = QgsMapLayerStyle()
style.readFromLayer(layer)

for qml_file in [f for f in os.listdir(qml_path)
                 if os.path.isfile(os.path.join(qml_path, f)) and
                 f.endswith('.qml')]:
    # get style name from file
    style_name = os.path.basename(qml_file).strip('.qml')
    # add style with new name
    style_manager.addStyle(style_name, style)
    # set new style as current
    style_manager.setCurrentStyle(style_name)
    # load qml to current style
    (message, success) = layer.loadNamedStyle(os.path.join(qml_path, qml_file))
    print message
    if not success:  # if style not loaded remove it
        style_manager.removeStyle(style_name)

You can run it in the QGIS python console or adapt to a processing script.

(Tested on current LTR version QGIS 2.18)