1

I'm trying to import some CSV with the same name of the file in a geopackage using PyQGIS before opening a project, but what I'm using doesn't work

I'm using this code:

from qgis.core import *
import glob
import os

def openProject():
    ruta = QgsProject.instance().absolutePath()
    tmp = ruta + '/tmp'
    try:
        for csv in glob.glob(tmp + '/*.csv'):
            rutacsv = 'file:///' + csv + '?delimiter=;'
            tabla = QgsVectorLayer(rutacsv, os.path.basename(csv), 'delimitedtext')

            opt = QgsVectorFileWriter.SaveVectorOptions()
            opt.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
            QgsVectorFileWriter.writeAsVectorFormat(tabla, ruta + '/cdau.gpkg|layername' + os.path.basename(csv).split('.csv', 1)[0], opt)

I tried also this code:

from qgis.core import *
import glob
import os

def openProject():
    ruta = QgsProject.instance().absolutePath()
    tmp = ruta + '/tmp'
    try:
        for csv in glob.glob(tmp + '/*.csv'):
            rutacsv = 'file:///' + csv + '?delimiter=;'
            tabla = QgsVectorLayer(rutacsv, os.path.basename(csv), 'delimitedtext')

            opt = QgsVectorFileWriter.SaveVectorOptions()
            opt.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
            opt.LayerName = os.path.basename(csv).split('.csv', 1)[0]
            QgsVectorFileWriter.writeAsVectorFormat(tabla, ruta + '/cdau.gpkg', opt)

Although if I use this code, the table is stored but with the name of the geopackage (cdau).

from qgis.core import *
import glob
import os

def openProject():
    ruta = QgsProject.instance().absolutePath()
    tmp = ruta + '/tmp'
    try:
        for csv in glob.glob(tmp + '/*.csv'):
            rutacsv = 'file:///' + csv + '?delimiter=;'
            tabla = QgsVectorLayer(rutacsv, os.path.basename(csv), 'delimitedtext')

            opt = QgsVectorFileWriter.SaveVectorOptions()
            opt.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
            QgsVectorFileWriter.writeAsVectorFormat(tabla, ruta + '/cdau.gpkg', opt)

But I'm not able to see where the mistake is.

2
  • what "doesn't work"? – Ian Turton Jun 8 '20 at 7:34
  • Sorry, the table is not created with the first two codes. With the third one is created with the same name as the geopackage, not with the name of the .csv file – Raúl Casado Jun 8 '20 at 7:37
1

the following code when run from the python window reads csvs from my \tmp folder and adds each of them to the cdau.gpkg (it doesn't create the cdau.gpkp that has to exist already), each layer when added to the gpkp is given the name of the original csv

import glob
import os
ruta = QgsProject.instance().absolutePath()
tmp = ruta +'/tmp'
for csv in glob.glob(tmp + '/*.csv'):
    rutacsv = 'file:///' + csv + '?delimiter=,'
    tabla = QgsVectorLayer(rutacsv, os.path.basename(csv), 'delimitedtext')
    opt = QgsVectorFileWriter.SaveVectorOptions()
    opt.EditionCapability = QgsVectorFileWriter.CanAddNewLayer
    opt.layerName = os.path.basename(csv).split('.csv', 1)[0]
    opt.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
    QgsVectorFileWriter.writeAsVectorFormat(tabla, ruta + '/cdau.gpkg', opt)
4
  • Thanks @ian for your answer, but if I add your code the table is imported in the geopackage, but with the name of the gpkg file. What I need is to import every csv in the tmp folder and name every new table with the name of the csv file. – Raúl Casado Jun 8 '20 at 16:14
  • Hi @RaúlCasado I've updated the code in my answer - it adds the csvs to the gpkg when run from the python window in QGIS 3.10 on Windows 10 – ian Jun 8 '20 at 16:57
  • Checking your code I saw that in the option line where the name of the new table is defined I wrote opt.LayerName = os.path.basename(csv).split('.csv', 1)[0] with LayerName with a capital L, so I wrote the name of the function correctly (with lowercase l) and it works fine. Thanks @ian. – Raúl Casado Jun 8 '20 at 18:06
  • @RaúlCasado, no problem, glad it's working now! – ian Jun 8 '20 at 19:32

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.