I have a function that uses a layer already styled using another function and saves it as a .gpkg and I want to save the style in that .gpkg:

def saving_gpkg(styled_layer):
    options = QgsVectorFileWriter.SaveVectorOptions()
    options.driverName = "GPKG"
    QgsVectorFileWriter.writeAsVectorFormatV2(styled_layer, "nice_filename", QgsCoordinateTransformContext(), options)
    styled_layer.saveStyleToDatabase("polygon", "style automatically created", True, "")

The .gpkg is created, but if I open the created file, it doesn't have the defined style, just a random single symbol. I'm missing something?

3 Answers 3


A working example based on your code. Be aware that my code scope is limited as I use a layer name without space for everything whereas I should sanitized it in particular for layername parameter reused in the GPKG. Most of the code logic is based on qgsalgorithmpackage.cpp C++ code used in Processing algorithm "Package layers" in the answer from @leo-cardona

# Import required if not present
from qgis.PyQt.QtXml import QDomDocument
from qgis.core import (QgsReadWriteContext, QgsProject,
                       QgsVectorFileWriter, QgsVectorLayer)

def saving_gpkg(styled_layer, out_path):
    context = QgsProject.instance().transformContext()
    name = styled_layer.name()
    options = QgsVectorFileWriter.SaveVectorOptions()
    options.layerName = name
    options.fileEncoding = styled_layer.dataProvider().encoding()
    options.driverName = "GPKG"
    QgsVectorFileWriter.writeAsVectorFormatV2(styled_layer, out_path, context, options)
    doc = QDomDocument();
    readWriteContext = context = QgsReadWriteContext()
    gpkg_layer = QgsVectorLayer(f"{out_path}|layername={name}", name, "ogr")
    gpkg_layer.saveStyleToDatabase(name, "", True, "")
  • I can confirm this works Using QGIS 3.16.6 :-), the only thing I needed to do was to import those extra functions: from qgis.PyQt.QtXml import QDomDocument, from qgis.core import QgsReadWriteContext and just a few renames like the input layer object and the output path. FYI my layers/datasets are in Postgres and my styles are also saved in Postgres.
    – GIS_py
    Aug 2, 2021 at 13:38
  • Edited to add all required import
    – ThomasG77
    Aug 2, 2021 at 14:18
  • The function above only seems to save the active style. Any idea how you could adapt this for multiple styles ? I sometimes have multiple styles applied to the same layer (saved in a postgres DB).
    – lhowarth
    Apr 15, 2022 at 13:43

If you want to save one or several layers in a geopackage, the easiest way is to use the package algorithm which has the SAVE_STYLES parameter which must be True if you want to save the styles that your layers have in the geopackage that will be generated.

filename = '/home/ai/Desktop/prueba.gpkg'
layers = [styled_layer]
processing.run("native:package", {'LAYERS': layers, 'OUTPUT': filename, 'OVERWRITE': False, 'SAVE_STYLES': True})

I developed a plugin that saves all styles with each layer as a .gpkg. Published on the official QGIS plugin repo: https://plugins.qgis.org/plugins/SaveAllScript/

  • 1
    This is not answering the question. The Q is about programming. Jun 30 at 8:08
  • I would like to clarify that this comment is relevant to the question because I created a QGIS plug-in that addresses some of the questions that the original poster had. I am sharing it here because maybe people in the future who also share the same questions can easily fix their problem with the plug-in. People are welcome to reference the code for the plug-in in order to help them with their own programming goals. Jun 30 at 23:17

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.