<div dir="ltr"><div dir="ltr"><div>Hi Jorge,</div><div><br></div><div>Have a look at this <a href="https://github.com/qgis/QGIS/blob/9cb272969945a2b6d63e224b81ab2cfafa595a9b/tests/src/python/test_qgslayoutlegend.py#L426">https://github.com/qgis/QGIS/blob/9cb272969945a2b6d63e224b81ab2cfafa595a9b/tests/src/python/test_qgslayoutlegend.py#L426</a></div><div><br></div><div>Though some things tested in the code are from one of my PRs stuck in limbo, this should be enough to get you started.</div><br clear="all"><div><div dir="ltr" class="gmail_signature"><div dir="ltr">Alex</div></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le ven. 22 nov. 2019 à 07:34, Jorge Tornero <<a href="mailto:jtorlistas@gmail.com">jtorlistas@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><table style="box-sizing:border-box;border-spacing:0px;border-collapse:collapse;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";font-size:14px"><tbody style="box-sizing:border-box"><tr style="box-sizing:border-box"><td id="gmail-m_6569704637137807118gmail-file-qgis-composer-scripts-LC10" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:12px;white-space:pre-wrap">
</td></tr><tr style="box-sizing:border-box"><td id="gmail-m_6569704637137807118gmail-file-qgis-composer-scripts-L17" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-m_6569704637137807118gmail-file-qgis-composer-scripts-LC17" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:12px;white-space:pre-wrap"></td></tr><tr style="box-sizing:border-box"><td id="gmail-m_6569704637137807118gmail-file-qgis-composer-scripts-L18" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-m_6569704637137807118gmail-file-qgis-composer-scripts-LC18" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:12px;white-space:pre-wrap"></td></tr></tbody></table>Hello all,<br>I'm trying to make a script with python (standalone, not in qgis console or whatever) for exporting composer maps to an image.<br><br>The idea is to have a template of a map and just change some layers, the title, etc., depending on the data I want to show. For instance, I can have a base map with a bathymetry layer, a geography (land) layer and then produce several maps with, say the abundance of a particular species of fish.<br>So far I'm able to:<br><br>1) Import qgis and create a QgsApplication<br><br>from qgis.core import *<br>QgsApplication.setPrefixPath("", True)qgs=QgsApplication([],False)qgs.initQgis()<br><br>2) Create and load a project from a file<br><br>pro=QgsProject()pro.read('./miproject.qgs')<br><br>3) Access to its layout manager and thus to one particular layout<br>pro=QgsProject()pro.read('./miproyecto.qgs')layoutManager=pro.layoutManager()<br>layout=layoutManager.layoutByName('MYLAYOUT')<br><br>4) And then export that layout to a file: <br><br>exporter=QgsLayoutExporter(layout)<br>exporter.exportToImage('mymap.png',exporter.ImageExportSettings()) <br><br><br>But I'm having big trouble trying to manipulate the legend itself.<br>I can get the legend with (I omit the listing of layout items to get the index of the legend):<br><br>legend=layout.items()[2]<br><br>But how could I access to the legend items and be able to modify them?<br><br>So far I know that I can access to the legend model through legend.model(), and then with say tree=lengend.model().rootGroup() I guess I get the legend tree. In fact I can begin to hear some music with dump() and I have had success up to some extent and even changing some text in the legend items, but it looks like the way I made it is far from being fair. For instance, for changin one particular name inside the legend I've made:<br><br>tree.children()[0].children()[0].setName('New Name')<br><br><br>But I can't figure out how to manipulate that tree for changing the legend matching the information layer corresponding to each particular map. It looks like it has something to do with accessing to the XML of the tree and manipulating, but my efforts with writeXML() and trying to get something from it led to nonsense.<br><br>So I will appreciate so much some clues about how to access and modify the legend of a print layout.<br><br>The last intention of this is to use thise scrips to include output from QGIS maps directly into RMarkdown documents. I consider that producinq maps with QGIS is by far more versatile than with R and I've made some test and it would work like a charm.<br><br>Thank you very much for your help, all the best<br><br>Jorge Tornero</div></div>
_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a></blockquote></div>