I am spliting 20-30 national datasets into 10 regions and want each region to inherit the style of the parent and have these saved in the geopackage of each new region layer (sort of like a function to 'copy paste style' but with the ability to nominate "save style to layer's geopackage".

At the moment I am doing this tediously by copy pasting style from the parent to the new 10 layers and editing each one through the layers' properties menu > Save style > Save Style in database > enter a style name > select 'use as defualt style for this layer' > apply > OK.

Is there an easier way?

1 Answer 1


I don't think it is the most straight forward way to do this but you could proceed like this:

national_dataset = QgsProject.instance().mapLayersByName("name_of_the_dataset")[0]

#Get the style of the national dataset layer:
style = national_dataset.styleManager().style(national_dataset.styleManager().currentStyle())

# loop through all your regions layer and apply style from the national dataset:

names = [layer.name() for layer in QgsProject.instance().mapLayers().values()]
filterednames = list(filter(lambda k: 'region' in k, names)) #I'm listing all the layers containing the word "region" in their names
for region_name in filterednames:
    region_layer= QgsProject.instance().mapLayersByName(region_name)[0]
    region_layer.styleManager().addStyle('give_a_name_to_the_style', style)

    filename = 'C:/Users/Users/Documents/region_' + region_name +'.gpkg'
    processing.run("native:package", {'LAYERS': region_layer, 'OUTPUT': filename, 'OVERWRITE': True, 'SAVE_STYLES': True})

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.