I have dozens of temporary Layers. I'd like to export them all as GeoPackages, but I am unsure how to go about doing so without doing each one individually.

I was reviewing this code Exporting several files at same time in QGIS, but it exports files as .shp.

I have tried changing .shp to .gkpg and ESRI Shapefile to GeoPackage, but the code generated errors.

I did verify, however, that I can get the original code to run:

myDir = 'C:/temp/'

for vLayer in iface.mapCanvas().layers():
    QgsVectorFileWriter.writeAsVectorFormat( vLayer, 
        myDir + vLayer.name() + ".shp", "utf-8", 
        vLayer.crs(), "ESRI Shapefile" )

2 Answers 2


Another solution is to use the "Package layers" tool as a Batch Process.



Try the Package layers tool, and you can export the styles and metadata.

The problem in your code is the name of the OGR driver. You must set the OGR driver to GPKG instead of GeoPackage.

myDir = ''

for vLayer in iface.mapCanvas().layers():
        myDir + vLayer.name() + ".gpkg",
  • Thank you for the suggestion. It looks like this may work as a workaround, but I would still prefer to find a solution in PyQGIS form as it will be quicker and easier for naming purposes. Much obliged if you have any suggestions in that form! Jun 20, 2022 at 19:37
  • @KateBlackwell, I updated my answer.
    – Mayo
    Nov 16, 2022 at 22:22

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.