<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Hi,<br>
<br>
This thread might help you to find a workaround :<br>
<a class="moz-txt-link-freetext" href="https://gis.stackexchange.com/questions/241213/how-to-get-a-composer-label-by-id-with-python-in-qgis">https://gis.stackexchange.com/questions/241213/how-to-get-a-composer-label-by-id-with-python-in-qgis</a><br>
<br>
<pre class="moz-signature" cols="72">--
Christophe
</pre>
<div class="moz-cite-prefix"><br>
Le 16/03/2018 à 16:56, KOHLMANN Hannes a écrit :<br>
</div>
<blockquote type="cite"
cite="mid:23DC76E788B5AE46BF97C68F781B7C2522D3E4AF@OVEKBG.ove.at">
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<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]-->
<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
style="font-family:Wingdings" lang="EN-GB">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>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
QGIS-Developer mailing list
<a class="moz-txt-link-abbreviated" href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a></pre>
</blockquote>
<br>
</body>
</html>