I have a .csv file containing financial data which is generated on a regular basis outside QGIS. Then I want to create a PyQGIS script that will add the .csv file as a layer in my project. (The idea here is I want to have the up-to-date financial data as attributes data for one of the shapefiles in the project).

Is there a way to run the script automatically as soon as I open the QGIS project?

  • Why not store the layer as a view in a database, and use your csv creation script to instead import the data to table as part of the view, the other part being the spatial component? – wildintellect Mar 17 '14 at 17:32

Put your script in .qgis/python/startup.py and it should run when QGIS is opened. However, this will run with any project you open, which you may not want.

So you might be better off setting up a python macro in the specific project. File > Project properties > Macros. Enable macrcos by checking the box.

def openProject():
    wack your code here!

def saveProject():

def closeProject():
  • 2
    +1 Marcos are the way to go. If you have a lot of code I would suggest putting it in it's own .py file next to the project and importing it from openProject rather then putting lots of logic in the blocks. Makes it easier to test and debug. – Nathan W Mar 17 '14 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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