from qgis.core import QgsProject project = QgsProject.instance() filename = '/path/to/project.qgs' success = project.read(filename) if success: print('Project loaded') else: print('Unable to load project')
from qgis.core import QgsProject project = QgsProject.instance() filename = '/path/to/save/project.qgs' success = project.write(filename) if success: print('Project saved') else: print('Unable to save project')
from qgis.core import QgsVectorLayer, QgsProject project = QgsProject.instance() layer_name = 'My Layer' source = '/path/to/shapefile.shp' layer = QgsVectorLayer(source, layer_name, 'ogr') if layer.isValid(): project.addMapLayer(layer) print('Layer added to project') else: print('Unable to add layer')In conclusion, the examples above illustrate some basic functionality of the QgsProject class in Python QGIS. The qgis.core package library is used to import the required classes and methods for manipulating QGIS projects and layers.