First time using QGIS modeler so i'm not familiar with it.

I want to select features from multiple layers (how many layers and what layers changes depending on the work) merge them into 1 layer (attribute only) then export to a spreadsheet.

I know i can select the features then right click each layer and save selected features as, then merge the resulting spreadsheets but that is slow and cumbersome. i also know i can run extract selected features in a batch process then run merge vector layers and output the result as a spreadsheet but that is also slow and cumbersome.

I'm looking at running the extract selected features in model builder with merge vector layers, the issue i'm facing is that I can't set extract selected features to allow for multiple inputs. If i run the model as a batch process each layer gets extract selected features run on it then each selection form each layer get merge vector layers run on it resulting in individual layers instead of 1 combined layer.

What is the best way to go about this? I tried setting the input for Extract selected features to a pre-calculated value of @map_layers but it errors out when running. I also tried the Multiple input but the Extract selected features doesn't recognize it as a valid input.

enter image description here

enter image description here

  • Do all layers have the same attributes? Do you want all attributes from each layer in the final output?
    – OGmaps
    Sep 29, 2023 at 21:00
  • @OGmaps No they do not have all the same attributes and yes i do want all the attributes.
    – Mow
    Oct 3, 2023 at 19:40

1 Answer 1


You asked how to do this using QGIS Model Builder - but it is likely simpler using pyqgis.

Executing the following code in the QGIS Python script editor will generate an xlsx file with fields from all selected features placed in a sheet with the corresponding name.

import json
import pandas as pd
from datetime import datetime

output_dir = r'C:temp'
filename = 'output_{}.xlsx'.format(datetime.now().strftime('%m%d%Y%H%M%S'))

writer = pd.ExcelWriter('{}/{}'.format(output_dir, filename))
for layer in QgsProject.instance().mapLayers().values():
    if layer.type() == QgsMapLayer.VectorLayer:
        if layer.selectedFeatureCount()> 0:
            selected = {f.id(): json.loads(QgsJsonUtils.exportAttributes(f)) for f in layer.getSelectedFeatures()}
            df = pd.DataFrame.from_dict(selected).T
            df.to_excel(writer, layer.name(),index=False)

Since each layer may or may not have the same fields - you will have to figure out a way to organize the data if a single worksheet is part of your requirement.

Example Output

enter image description here

Tested on QGIS v3.28

  • This works perfectly. I had a bunch of trouble installing openpyxl in my QGIS environment. For anyone that has the same issue open your OSGeo4W shell and run python -m pip install openpyxl. That was how i was able to install openpyxl in my QGIS python environment.
    – Mow
    Oct 5, 2023 at 23:17
  • I've tried to save this script in my processing toolbox but it doesn't show up. Is there anything that needs to be added to the script so it can be saved in the processing toolbox? I have made sure the processing options is looking at the folder where the script is saved.
    – Mow
    Oct 5, 2023 at 23:40
  • When i run the script in the python console it works. But if i run it by left clicking the python icon in processing toolbox and hitting create new script then copying the code into it doesn't work and produces several errors.
    – Mow
    Oct 5, 2023 at 23:48

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.