QGIS – Batch Export GeoPackage Attribute Table to CSV

attribute-tablecsvexportqgisqgis-3

I have a large quantity (200+) of GeoPackage files, and I want to export the attributes table of all of them to CSV's. Is there a way I can do that in the GUI or with a Plugin for QGIS 3.16.15?

Alternatively, is there a Python script I can use to export them? I'm not too comfortable with Python so would prefer a Plugin if it exists, but if not I can run the Python script & input the file path/destination folder.

Best Answer

To export all layers opened in QGIS i created this little Python script:

from qgis.core import *

#Put your folder where you want your CSV exported to here
 pathToFile = "C:/Path/To/Export/Folder/"
 layers = QgsProject.instance().mapLayers().values()
 for lyr in layers:
     newName = lyr.name() + ".csv"
     save_options = QgsVectorFileWriter.SaveVectorOptions()
     save_options.driverName = "CSV"
     save_options.fileEncoding = "UTF-8"
     transform_context = QgsProject.instance().transformContext()
     ret = QgsVectorFileWriter.writeAsVectorFormatV2(lyr, pathToFile + newName, transform_context,save_options)
Related Question