layer = QgsVectorLayer("/path/to/shapefile.shp", "LayerName", "ogr") field = QgsField("NEW_FIELD", QVariant.String) layer.addAttribute(field) layer.updateFields()
layer = QgsVectorLayer("/path/to/shapefile.shp", "LayerName", "ogr") fields = [ QgsField("NEW_FIELD_1", QVariant.Int), QgsField("NEW_FIELD_2", QVariant.Double) ] layer.dataProvider().addAttributes(fields) layer.updateFields()This example adds two new attribute fields with the names "NEW_FIELD_1" and "NEW_FIELD_2" to an existing shapefile layer. The types of the new fields are set to Int and Double using the QVariant.Int and QVariant.Double values, respectively. The addAttributes method is called on the layer's data provider, and the updateFields method is called to apply the changes to the layer. Package library: qgis.core