I need to save the symbology of several layers and I would like to do it with a PyQGIS-3 script.

I think I have to use the QgsMapLayerStyle class but I don't understand what is a QDocument and how to use this class

niv1 = QgsProject.instance().mapLayersByName('exempleNiveau1_UniteDiagnostic_CMH_valeur normalise')[0]
doc = QDomDocument("testdoc") # path to qml/xml document to create ?
elem = doc.createElement("maplayer") # layer with the symbology to save ? Vector layer is not supported
niv1.writeXml(elem, doc, QgsReadWriteContext())

doc = QDomDocument("C:/Users/Juliette/Documents/CRECQ/CartesIndicateurs/Layers/testdoc")
elem = doc.createElement("exempleNiveau1_UniteDiagnostic_CMH_valeur normalise")
niv1.writeXml(elem, doc, QgsReadWriteContext())

You can make use of the saveNamedStyle function:

layer = iface.activeLayer()
bckup_fileName = bckupFolder + layer.name() + ".qml"

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.