I'll give you an example, you can adapt it to your case and optimize it.
My starting table with 2 fields: the first one an identifier and the second the numerical value corresponding to the number of duplicates.
The code I execute in QGIS python console
layer = iface.activeLayer() # my layer
provider = layer.dataProvider() # access to the features of the layer
# Loop on the features of the layer
for feature in layer.getFeatures():
id_duplicate = feature["id"] # value of the field id
number_duplicate = feature["number"] # number of duplicates
geometry_duplicate = feature.geometry() # geometry of my feature
# initialization of my counter which will allow me to know when I should stop duplicating
counter = 0
while counter != number_duplicate :
new_feature = QgsFeature() # new feature
new_feature.setAttributes([id_duplicate,number_duplicate]) # I apply the same attributes
new_feature.setGeometry(QgsGeometry(geometry_duplicate)) # I apply the same geometry
provider.addFeature(new_feature) # Addition of the new entity
counter += 1
# if the value of my field is equal to 1 then I stop duplicating this entity
and move on to the next one otherwise I continue.
My end table
Feel free to ask for more information if you don't understand the code. If you want a multigeometry and do not duplicate the lines, you can use the dissolved tool with the grouping field "id".