Distrotech / reportlab

Mirror of https://bitbucket.org/rptlab/reportlab
Other
59 stars 42 forks source link

Paragraph() bug? #13

Closed sunshinewithmoonlight closed 4 years ago

sunshinewithmoonlight commented 4 years ago

Hello, my question is about reportlab.

When i run the following python code:

from reportlab.platypus import Paragraph
from reportlab.lib.styles import getSampleStyleSheet

a='0.1 <A <0.9'
story = [Paragraph(a, getSampleStyleSheet())]

An error occurred:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in 
      3 
      4 a='0.1 
----> 5 story = [Paragraph(a, getSampleStyleSheet())]

~\AppData\Local\Programs\Python\Python37\lib\site-packages\reportlab\platypus\paragraph.py in __init__(self, text, style, bulletText, frags, caseSensitive, encoding)
   1539         self.caseSensitive = caseSensitive
   1540         self.encoding = encoding
-> 1541         self._setup(text, style, bulletText or getattr(style,'bulletText',None), frags, cleanBlockQuotedText)
   1542 
   1543 

~\AppData\Local\Programs\Python\Python37\lib\site-packages\reportlab\platypus\paragraph.py in _setup(self, text, style, bulletText, frags, cleaner)
   1561             _parser = ParaParser()
   1562             _parser.caseSensitive = self.caseSensitive
-> 1563             style, frags, bulletTextFrags = _parser.parse(text,style)
   1564             if frags is None:
   1565                 raise ValueError("xml parser error (%s) in paragraph beginning\n'%s'"\

~\AppData\Local\Programs\Python\Python37\lib\site-packages\reportlab\platypus\paraparser.py in parse(self, text, style)
   3222             self.feed(text)
   3223         except:
-> 3224             annotateException('\nparagraph text %s caused exception' % ascii(text))
   3225         return self._complete_parse()
   3226 

~\AppData\Local\Programs\Python\Python37\lib\site-packages\reportlab\lib\utils.py in annotateException(msg, enc)
   1395         A.append(msg)
   1396     v.args = tuple(A)
-> 1397     rl_reraise(t,v,b)
   1398 
   1399 def escapeOnce(data):

~\AppData\Local\Programs\Python\Python37\lib\site-packages\reportlab\lib\utils.py in rl_reraise(t, v, b)
    148         if v.__traceback__ is not b:
    149             raise v.with_traceback(b)
--> 150         raise v
    151     def rl_add_builtins(**kwd):
    152         import builtins

~\AppData\Local\Programs\Python\Python37\lib\site-packages\reportlab\platypus\paraparser.py in parse(self, text, style)
   3220             text = u""+text+u""
   3221         try:
-> 3222             self.feed(text)
   3223         except:
   3224             annotateException('\nparagraph text %s caused exception' % ascii(text))

~\AppData\Local\Programs\Python\Python37\lib\html\parser.py in feed(self, data)
    109         """
    110         self.rawdata = self.rawdata + data
--> 111         self.goahead(0)
    112 
    113     def close(self):

~\AppData\Local\Programs\Python\Python37\lib\html\parser.py in goahead(self, end)
    169             if startswith('<', i):
    170                 if starttagopen.match(rawdata, i): # < + letter
--> 171                     k = self.parse_starttag(i)
    172                 elif startswith(", i):
    173                     k = self.parse_endtag(i)

~\AppData\Local\Programs\Python\Python37\lib\html\parser.py in parse_starttag(self, i)
    343             self.handle_startendtag(tag, attrs)
    344         else:
--> 345             self.handle_starttag(tag, attrs)
    346             if tag in self.CDATA_CONTENT_ELEMENTS:
    347                 self.set_cdata_mode(tag)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\reportlab\platypus\paraparser.py in handle_starttag(self, tag, attrs)
   3243             start = self.start_unknown
   3244         #call it
-> 3245         start(attrs or {})
   3246 
   3247     def handle_endtag(self, tag):

~\AppData\Local\Programs\Python\Python37\lib\site-packages\reportlab\platypus\paraparser.py in start_para(self, attr)
   2908 
   2909     def start_para(self,attr):
-> 2910         frag = self._initial_frag(attr,_paraAttrMap)
   2911         frag.__tag__ = 'para'
   2912         self._stack = [frag]

~\AppData\Local\Programs\Python\Python37\lib\site-packages\reportlab\platypus\paraparser.py in _initial_frag(self, attr, attrMap, bullet)
   2887             frag.textColor = hasattr(style,'bulletColor') and style.bulletColor or style.textColor
   2888         else:
-> 2889             frag.fontName, frag.bold, frag.italic = ps2tt(style.fontName)
   2890             frag.fontSize = style.fontSize
   2891             frag.textColor = style.textColor

AttributeError: 'StyleSheet1' object has no attribute 'fontName'
paragraph text '0.1 ' caused exception
sunshinewithmoonlight commented 4 years ago

Sorry to bother you, I found a solution to this problem: https://stackoverflow.com/questions/19712130/python-reportlab-unable-to-print-special-characters/19719908#19719908

You should replace &, < and > with the &, < and >. One easy way to do that is the Python escape function: from cgi import escape Story.append(Paragraph(escape("Some other text with &"), styles["Normal"])) However, HTML tags need to have real < and > so a typical use would be more like: text = "Some other text with &" Story.append(Paragraph(escape("" + text + ""), styles["Normal"]))

You can also do like this:

story = [Paragraph(a.replace(r'&',r'&amp;').replace(r'<',r'&lt;').replace(r'>',r'&gt;'), styles['font_msyh'])]