6

We work on a large number of projects and the file structure is normally the same so repeatedly browsing for the right location to save plans has issues. I would like to be able to change the default paths for saving layouts to PDFs, SVGs and images based on the project file path.

Following Changing default folder for saving layers in QGIS I can change the default path for saving vector layers by running the following code in the project macros.

import os
from qgis.core import QgsProject
from PyQt5.QtCore import QSettings

prjhomepath = QgsProject.instance().homePath()
if os.path.exists(prjhomepath):
   QSettings().setValue('/UI/lastVectorFileFilterDir', prjhomepath)

I assumed there might be a setting for file saving similar to '/UI/lastVectorFileFilterDir' but the layout export directory seems to be a property of the project.

Saving the project file as a .qgs and examining the text file reveals a line that seems to be what I need.

Project file

If I change that text string (in Notepad) it successfully changes the default file path for exporting layouts.

How can I change that string with PyQGIS?

0

1 Answer 1

7

You can use writeEntry method of QgsProject.

QgsProject.instance().writeEntry("Layouts",                     # scope
                                 "/lastLayoutExportDir",        # key
                                 "C:/Users/user/Desktop/Folder" # value
                                )

enter image description here

4
  • writeEntry does change the value for the key as desired, but that doesn't change the default location when I go to export a layout, maybe its due to the difference between .qgs and .qgz?
    – Sethinacan
    Commented Aug 28 at 7:41
  • 1
    It changed the location when I go to export a layout in macos. I haven't test on other OSs. Commented Aug 28 at 8:22
  • Thanks for checking I'll try again later
    – Sethinacan
    Commented Aug 28 at 10:14
  • The issue on windows seem to be related to the format of the string for the value. So some file paths work and others don't - The project file path will be fine so I probably need to get that in a format that will work. [Retrieve path and file name of current QGIS project in Python]gis.stackexchange.com/questions/34005/…
    – Sethinacan
    Commented Aug 28 at 14:41

Your Answer

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

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