1

I need to save a style file (.qml) that will only contain the "Symbology", "Labeling", "Forms" categories. With PyQGIS I can save a named style like this:

lyr = iface.activeLayer()
path = f'C:\\Users\\Path\\To\\Save\\Directory\\{lyr.name()}.qml'
if lyr.type() == QgsMapLayerType.VectorLayer:
    lyr.saveNamedStyle(path)

How can I save a .qml style file with a subset of style categories?

0

1 Answer 1

4

Just pass the style categories you wish to save after the categories keyword argument. It should go without saying that you just need to change the path variable for your own system.

lyr = iface.activeLayer()
path = f'C:\\Users\\Path\\To\\Save\\Directory\\{lyr.name()}.qml'
if lyr.type() == QgsMapLayerType.VectorLayer:
    lyr.saveNamedStyle(path, categories = QgsMapLayer.Symbology | QgsMapLayer.Labeling | QgsMapLayer.Forms)

Tested in QGIS 3.20

You can read the docs here and here.

0

Your Answer

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

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