Applying field calculator for multiple layers at once

You can do this by making 2 loops: the first on the layers of your project and the second on the features of each layer.

# Project layers 
project_layers = QgsProject.instance().mapLayers().values()

# Loop on each layer 
for layer in project_layers :
    layer.startEditing()
    layer.dataProvider().addAttributes([QgsField("area", QVariant.Double)])
    layer.updateFields()

    # Loop on each feature 
    for feature in layer.getFeatures():
        area = feature.geometry().area()
        feature.setAttributes([area])
        layer.updateFeature(feature)

    # Apply changes
    layer.commitChanges()

An alternative solution to using written Python code would be to use the Graphical modeller to create a small script. Just set a Vector-Layer as input and add the field calculator as step with $area as a formula. Something like this:

A small script for a field calculator operation

You can then run the script in Batchmode and do the same operation for several layers at once.


You can run the Field Calculator in Batch Processing Mode directly from the Processing Toolbox.

Right click on the tool name and choose "batch processing mode."

enter image description here

Or you can run add geometry attributes in batch processing mode from the processing toolbox. This method will add more geometry attributes than just area.