[reportlab-users] Paragraph text with &

Robin Becker robin at reportlab.com
Sat Feb 3 07:38:35 EST 2007


Juha Ruotsalainen wrote:

> Hi there,

>

> Juha Tuomala's hint to use "text = text.replace( '&', '&')" didn't

> work. I guess it has something to do with Python's XML parser (that's

> what Paragraph uses).

>

> See, my paragraph's contents are read from a XML file, where I have a

> text "<subject>Jack, Jim &amp; Jill</subject>". After parsing the

> input XML via xml.dom.minidom, "&amp;" has been converted to "&".

>

> Now if I use "&amp;".form for Paragraph's constructor, it is being fed

> through XML parser, which does the same, i.e. replaces "&amp;" with a

> "&", I'm back where I started: Reportlab raises (a deprecated, at

> least in Python 2.5, text-based exception), because a line starts with

> an ampersand.

>

> Slainte,



If you really want the text "<subject>Jack, Jim &amp; Jill</subject>" to
appear in a pdf document using Paragraph then you need to actually have
the text

&lt;subject&gt;Jack, Jim &amp;amp; Jill&lt;/subject&gt;

you can do this in python with

>>> from xml.sax.saxutils import escape
>>> print escape("<subject>Jack, Jim &amp; Jill</subject>")
&lt;subject&gt;Jack, Jim &amp;amp; Jill&lt;/subject&gt;
>>>


ie the escape function puts the standard xml entities into a form
suitable for parsing

--
Robin Becker


More information about the reportlab-users mailing list