Friday 23 September 2016

pyqgis - Why do I get a NameError for QDomDocument when attempting to programmatically load a template in the QGIS Python console?


I'm trying to work through this answer in the QGIS (2.14) Python console to understand, ultimately, how to iterate over layers and print them with a template.



When I run the following code block.


I get a NameError that QDomDocument() is undefined.


myComposition = QgsComposition(myMapRenderer)
myFile = os.path.join(os.path.dirname(__file__), 'MMR_Template.qpt')
myTemplateFile = file(myFile, 'rt')
myTemplateContent = myTemplateFile.read()
myTemplateFile.close()
myDocument = QDomDocument()

Possibly related (can split into a separate question) I also get the warning DeprecationWarning: QgsComposition constructor is deprecated but can find nothing about this warning on Google.




Answer



From this comment, adding the following line to the console script resolved this issue in 2.14 to 2.18.2


from qgis.PyQt.QtXml import QDomDocument

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...