2

I want to save my layout. I've tried this solution but and it worked. The file is created but when I try to open it, it doesn't work. I don't know why.

Here is my code:

project = QgsProject.instance()
manager = project.layoutManager() 
layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName(layoutName)
manager.addLayout(layout)
pc = layout.pageCollection()
page = pc.pages()[0]
page.setPageSize(pageSize, QgsLayoutItemPage.Orientation.Landscape)
layout_view = open_designer.view()
layout.saveAsTemplate('C:/Users/ilyasse2.0/Desktop/SQRLAND/layout.qpt',QgsReadWriteContext())

The error I get is :

Invalid Data Source: C:\Users\ilyasse2.0\Desktop\SQRLAND\layout.qpt is not a valid or recognized data source.

1 Answer 1

1

I've tried your first code and got some issues. An edited version below

pageSize = "A4"
layoutName = 'whatever'

project = QgsProject.instance()
manager = project.layoutManager() 
layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName(layoutName)
manager.addLayout(layout)
pc = layout.pageCollection()
page = pc.pages()[0]
page.setPageSize(pageSize, QgsLayoutItemPage.Landscape) # Changed here
# Path changed because Linux (keep your own path for Windows)
layout.saveAsTemplate('/tmp/layout.qpt', QgsReadWriteContext())

Then, I'm able to load the qpt file. What version of QGIS are your using? How do you proceed to load the qpt file?

4
  • i am using the latest version of QGIS 3.20.1 to load the qtp file i just go to my directory where it's saved and double click it Commented Aug 16, 2021 at 9:29
  • i have tried your code but still not working showing the same problem : invalid ... Commented Aug 16, 2021 at 9:30
  • "I just go to my directory where it's saved and double click it" Will not work this way. Go to menu "Project" > "Layout Manager". In new opened Windows, you will find "New from template". In drop down list, choose "Specific", then you can browse to select the created qpt file. After, click "Create". It will ask you to choose a new layout name and you are done.
    – ThomasG77
    Commented Aug 16, 2021 at 10:46
  • i got you it's working now thank you Commented Aug 18, 2021 at 9:22

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.