I am trying to make a script that reads the content of a .dbf-file and exports it as a shapefile. Equal to the manual pattern "Save as.." when rightclicking the imported layer in the Qgis Desktop.

I've read the Cookbook for developing in pyqgis and tried to troubleshoot with the documentation of QgsVectorFileWriter() but it seems to be written for C++ implementation and not for Python.

 _VctLay = QgsVectorLayer("path-to-file\file_imp.dbf", "Import_Area", "delimitedtext")
 QgsVectorFileWriter.writeAsVectorFormat(_VctLay ,"path-to-file\file_exp","CP1252",None,"ESRI Shapefile")

I'd like it to be a Shapefile but it is only the .dbf-file i have to use. Moreover I want the CRS to be "EPSG:4095" but it makes an error if i type it in like that. I've tried to have the "bool onlySelected = True" and False" - but none is adding content to the exported table.

I am getting an output. The whole package of files that follows with a shapefile. But they are about all empty. It only creates the files themselves.

Is there some flag, some process i am missing out on this? I wonder if i could have the first step. _VctLay = QgsVectorLayer("path-to-file\file_imp.dbf", "Import_Area", "delimitedtext") to be followed by an print-commands that clarifies the content of the imported .dbf. But exactly how, i don't know yet.

1 Answer 1


Bit of a convoluted process but this should work (make dbf layer vector layer, convert dbf to csv, then convert csv to delim layer, and finally convert delim layer to shp):

DBFlyr = QgsVectorLayer(myDBFPath, 'anyName', "ogr")
myCSVPath = 'C:/Temp/myCSV.csv'
QgsVectorFileWriter.writeAsVectorFormat(DBFlyr, myCSVPath, "utf-8", None, "CSV")
uri='file:///'+ myCSVPath + '?delimiter=,&yField={}&xField={}'.format('y_fieldName', 'x_fieldName')
delimLYR = QgsVectorLayer(uri, 'myDelimLayerName', 'delimitedtext')
QgsVectorFileWriter.writeAsVectorFormat(delimLYR,'C:/Temp/new.shp',"CP1252",None,"ESRI Shapefile")
  • How would QgsVectorLayer be structured if the .dbf had no geographic x.y-coordinates as input? (jf. uri='file:///'+ myCSVPath + '?delimiter=,&yField={}&xField={}'.format('y_fieldName', 'x_fieldName') ) I imagine something like ('null', 'null') or a tag that states "no geometry (Only attribute table)"
    – MichaelR
    Jul 20, 2016 at 7:08
  • To create a shp you need some sort of geographic coordinates/information. If your shp layer is type point, then you will need to add the x/y fields/values to the dbf. If the shp is type polyline or polygon then you will have to investigate more into the WKT route.
    – artwork21
    Jul 20, 2016 at 12:00
  • The equivalent to the "Save as..." function when right-clicking the delimited text layer, that was added without any geospatial reference, ie a single dbf-file, is having a possibility to select the output as DBF-file. That is what i am trying to find a way in pyqgis to automate. Among the gdal package "shp2pgsql" it takes an argumentinput that is describes as an shapefile, but it can be flagged to accept the .dbf-extension instead. Maybe it isn't possible in QgsVectorLayer.
    – MichaelR
    Jul 20, 2016 at 12:53

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.