I need to create a stand-alone script for opening a QGIS template project, modify a layer data source, export the Composition and finally save the modified .qgs project into a new file. So far I have only found some examples for QGIS plugins, but of course they don't work for the stand-alone uses. Can anybody give me some helpful hints? Thanks!

1 Answer 1

  1. you can use PyQGIS from outside QGIS (search in gis.stackexchange)
  2. otherwise, a QGIS project file ( .qgs) is a simple XML file and there are many Python modules to process XML files:

part of a .qgs/XML file:

from xml.etree import ElementTree as ET 
with open('test.qgs', 'r') as f:
     tree  = ET.parse(f)
     for elem in tree.findall('.//mapcanvas'):
         print("units: "), elem.find('units').text
 units :  meters
  • Thanks gene! Actually my question was already focused on the PYQGIS solution. Learning from several stand-alone scripts thant I have found online I manage to create brand new compositions, add layers and export the maps. I haven't found the right way to open a qgs project and work on it, yet.
    – Andreampa
    Jun 27, 2014 at 9:51
  • to open a qgis project : project = QgsProject.instance() project.read(QFileInfo('pathtoQgisproject.qgs')) Sep 12, 2016 at 8:54
  • @Andreampa do you got the solution ?if yes please post the solution
    – Adithya
    Jan 2, 2021 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.