I have a lot of .TAB files open in QGIS and I want to save them to .SHP so I can work with them in ArcMap.
Is there a way to save/export all these files at the same time or do I really have to "save as" separately for every single file?
I have a lot of .TAB files open in QGIS and I want to save them to .SHP so I can work with them in ArcMap.
Is there a way to save/export all these files at the same time or do I really have to "save as" separately for every single file?
Try this @Frida:
Create a folder to store your Shapefiles there (e.g., I've created the folder /tmp/data/
, I use GNU/Linux).
In QGIS, open the QGIS Python Console (Ctrl+Alt+P).
Write the following line, editing the right hand side to match the full path to your folder (make sure you include the trailing slash/backslash):
myDir = '/tmp/data/'
Press Enter.
Copy the following lines to the QGIS Python console:
for vLayer in iface.mapCanvas().layers():
QgsVectorFileWriter.writeAsVectorFormat( vLayer,
myDir + vLayer.name() + ".shp", "utf-8",
vLayer.crs(), "ESRI Shapefile" )
Press Enter a couple of times.
You should now have your Shapefiles inside the folder you created in step 1.
If you face troubles, let me know your OS and the full path to your folder.
from qgis.core import *
suffix = "_foo"
pathToFile = "/path/to/wherever/"
layers = iface.legendInterface().layers()
for layer in layers:
newName = layer.name() + suffix + ".shp"
ret = QgsVectorFileWriter.writeAsVectorFormat(layer, pathToFile + newName, "utf-8", None, "ESRI Shapefile")
if ret == QgsVectorFileWriter.NoError:
print newName + " saved to " + pathToFile + "!"
For Raster Layer use the QgsRasterFileWriter
P.S.: You can find a more detailed example in the PyQGIS Developer Cookbook.
Just a little addition: If you would like to change the CRS of the exported file as well and add some more prefix and suffix you can alter the script like so:
import os
from qgis.core import *
pathToFile = "S:\\pathway\\"
trs = QgsCoordinateReferenceSystem()
trs.createFromId(31370)
suffix = "_Lambert1972_Versie2016-01-04"
prefix = "Transect_Vuursalamander_"
layers = iface.legendInterface().layers()
for layer in layers:
newName = prefix + layer.name() + suffix + ".shp"
ret = QgsVectorFileWriter.writeAsVectorFormat(layer,pathToFile + newName,'utf-8',trs,'ESRI Shapefile')
if ret == QgsVectorFileWriter.NoError:
print newName + " saved to " + pathToFile + "!"
The 31370 can be replaced by the EPSG of the CRS you would like the exported file to be in.
is this also possible with .gpkg-Layers to .xlsx?
I tried:
for vLayer in iface.mapCanvas().layers():
QgsVectorFileWriter.writeAsVectorFormat( vLayer,
myDir + vLayer.name() + ".xlsx", "utf-8",
vLayer.crs())
But I receive this error message:
'Creation of data source failed (OGR error: sqlite3_open(P:\x01_Projektordner\x819\x819128_IEK_Augsburg\x05_Daten\\GIS\\Shape\\FNP_bestand_nach Gemeinden_Tabellengemeinde_Adelsried.xlsx.gpkg) failed: unable to open database file)')