1

I am trying to write a script that exports all my layouts by their names as separate PDF files at specific directory.

I am beginner at programming, though.

I was trying to use 'for' loop for exporter.exportpdf() but there was a syntax error.

Here's the code

exporter = QgsLayoutExporter(layout)
for layout in manager.printLayouts():
   string sciezka = 'file_path'
   exporter.exportToPdf(string sciezka + layout.getName(), QgsLayoutExporter.PdfExportSettings())

EDITV2:

saving the PDF in the project's directory

manager = QgsProject.instance().layoutManager()
for layout in manager.printLayouts():
    exporter = QgsLayoutExporter(layout)
    home_path = QgsProject.instance().absoluteFilePath()
    exporter.exportToPdf(home_path.format(layout.name()),
    QgsLayoutExporter.PdfExportSettings())
2
  • The syntax error was to try to use Java syntax in Python. You should always include the complete error in the Question body as text.
    – Vince
    Commented Sep 27, 2022 at 23:07
  • You can find the steps to this approach on Anita Graser's blog: anitagraser.com/… Commented Sep 27, 2022 at 23:23

1 Answer 1

1

I read "manager.printLayouts" so I guess it's exporting cards from the print manager. if that's the case, this code exports each layout to a specific pdf

manager = QgsProject.instance().layoutManager()
for layout in manager.printLayouts():
    exporter = QgsLayoutExporter(layout)
    exporter.exportToPdf("F:{}.pdf".format(layout.name()),
    QgsLayoutExporter.PdfExportSettings())

(F: is the name of my drive)

2
  • 1
    That's exactly what I was looking for. :) Thank you very much
    – Proelior
    Commented Sep 28, 2022 at 22:23
  • 1
    @Proelior Please mark the question as resolved
    – fcka
    Commented Sep 13, 2023 at 12:19

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.