I am working in QGIS. I have a shapefile and its table and I need to export each row as a new file called "Fecha" field:


Finally, I will get 6 shapefiles such as 20130612.shp, 20130714.shp, ...etc


2 Answers 2


Open the Processing Toolbox and try the 'Split vector layer' tool.

Set the Unique "ID" field to Fecha and the Output file type to shp (or another format if you wish).


A solution using the PyQGIS.

Let's assume there is a point layer called 'random_points_test' with its corresponding attribute table, see image below


Do not forget to change the layer and field names at the bottom of the code, i.e. split_layer('CMillor_ldcRefAuto_2013_WGSUTMH31','Fecha')

Proceed with Plugins > Python Console > Show Editor > New Editor and paste the script below

# imports
import os, sys

# setting up the input layer and defining required parameters
def split_layer(layer_name, field_name):
    # loading layer
    layer = QgsProject.instance().mapLayersByName(layer_name)[0]
    layer_fields = layer.fields()
    idx_of_that_field = layer_fields.indexFromName(field_name)

    if not layer.isValid():
        raise ValueError("Layer failed to load!")

    # getting a set with unique key attributes
    list_attributes = []
    for feat in layer.getFeatures():
    list_attributes = set(list_attributes)
    # checking a number of features in the input layer
    if layer.featureCount() < 2:
        raise ValueError("Layer contains only one feature. No reason to double save it")

        # getting a working dir, where the input layer is stored
        path_to_file = layer.dataProvider().dataSourceUri()
        working_dir = os.path.split(path_to_file)[0]

        # looping over attributes from the set, selecting features and saving them as a new single layer
        for i in list_attributes:
            selection = layer.selectByExpression('{0}=\'{1}\''.format(field_name, i))
            writer = QgsVectorFileWriter.writeAsVectorFormat(layer, working_dir + "/{}.shp".format(i), "UTF-8", layer.crs(), driverName = "ESRI Shapefile", onlySelected = True)



Press Run script run script and get the output like this


A name of each output will match a pattern like '<attribute_value>.shp' e.g. 'Test 2', and should not '<attribute_field_name><attribute_value>.shp' i.e. 'Fecha_20130612.shp' etc.

If you want to change the directory where the output should be saved, please create this folder beforehand, see this thread for more details. And do not forget to apply changed in the writer = ....



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.