<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Vorformatiert Zchn";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
span.E-MailFormatvorlage17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
span.HTMLVorformatiertZchn
        {mso-style-name:"HTML Vorformatiert Zchn";
        mso-style-priority:99;
        mso-style-link:"HTML Vorformatiert";
        font-family:"Courier New";
        mso-fareast-language:DE;}
.MsoChpDefault
        {mso-style-type:export-only;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 70.85pt 2.0cm 70.85pt;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:2038965309;
        mso-list-type:hybrid;
        mso-list-template-ids:-1912591874 67567633 67567641 67567643 67567631 67567641 67567643 67567631 67567641 67567643;}
@list l0:level1
        {mso-level-text:"%1\)";
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0cm;}
ul
        {margin-bottom:0cm;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="DE" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Hello!<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span lang="EN-GB">I am facing difficulties trying to set the text of a  QgsLayoutItemLabel.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">I do something like the following to load a .qpt composer template and print this to a .png file (which works)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">path = "C:\\QGIS3\\apps\\qgis\\composer_templates"<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">template_file = open(path+"\\ANSR_print_composer2.qpt")<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">template_content = template_file.read()<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">template_file.close()<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">document = QDomDocument()<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">document.setContent(template_content)<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">composition = QgsLayout(QgsProject.instance())<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">composition.loadFromTemplate(document, QgsReadWriteContext())<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">map_item = composition.itemById('map')<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">legend_item = composition.itemById('legend')<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">text_item = composition.itemById('textbox')<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB">QgsLayoutExporter(composition).exportToImage( path+"\\report.png", QgsLayoutExporter.ImageExportSettings())<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">And now I’d like to modify only the text of the QgsLayoutItemLabel, which is the “textbox”:<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span lang="EN-GB"><span style="mso-list:Ignore">1)<span style="font:7.0pt "Times New Roman"">     
</span></span></span><![endif]><span lang="EN-GB">I can move it around with setX(), setY(), but I don’t know how to edit the text of the label. I thought of something like text_item.setText() or text_item.setLabelText(), but I think that text_item is just an
 instance of QgsLayoutItem, not QgsLayoutItemLabel. And there I get stuck.<o:p></o:p></span></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span lang="EN-GB"><span style="mso-list:Ignore">2)<span style="font:7.0pt "Times New Roman"">     
</span></span></span><![endif]><span lang="EN-GB">How would you manually add another text label, set the main property etc, such that, at the end of the day, my above code would export a .png containing this QgsLayoutItem?<br>
I would have begun this way: <br>
new = QgsLayoutItemLabel(composition)<br>
new.setText(“New label”)<br>
But it does not quite work that way!<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Would be of great help, if you could give me some hints on that.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Thank you in advance! </span><span lang="EN-GB" style="font-family:Wingdings">J</span><span lang="EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Hannes<o:p></o:p></span></p>
</div>
</body>
</html>