0

Using QGIS 3.8, is there anyway to copy a layer group (and its subgroups) from project1.qgz to project2.qgz? I'm trying to write an script that adds a layer group (with several layers and group layers in it) present in a qgz project to other qgz projects.

I know I can drag and drop the group to another project, but I'd like to do this programmatically, as I need to repeat this process several times.

1 Answer 1

1

For me this snippet works, but I use another QGIS/PyQGIS Version (QGIS 3.34.9):

from qgis.core import QgsProject

source_project_path = r"C:\Geodaten\test.qgz"
target_project_path = r"C:\Geodaten\test1.qgz"

# Read the Source Project
source_project = QgsProject.instance()
source_project.read(source_project_path)

# Clone the Layer Group
group_to_copy = source_project.layerTreeRoot().findGroup('your_group')
cloned_group = group_to_copy.clone()

# Read the Target Project
target_project = QgsProject.instance()
target_project.read(target_project_path)

# Insert Cloned Group in Target Project
target_root = target_project.layerTreeRoot()
target_root.insertChildNode(-1, cloned_group)

# Save Changes
target_project.write(target_project_path)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.