[reportlab-users] Problem parsing file

shawn a boris317 at gmail.com
Thu Jan 24 12:44:58 EST 2008


Its failing to open the image because you are passing the string
representation of the image node(tags and all) to the Image object,
which is then trying to open it.

> IOError: Cannot open resource "<image>

> C:\workspace\pythonlibs\tests\document\test_img.jpg

> </image>



I believe you are looking for something more like this

file = open("violation.tmpl", "r")
tree = parse(file)
elem = tree.getroot()
for para in elem.findall('para'):
contents.append(Paragraph(ElementTree.tostring(para),
paragraphStyle, None))
for image in elem.findall('image'):
contents.append(Image(image.data))

Ive replaced ElementTree.tostring(image)
with
image.data
this should return the text in BETWEEN the image tags.(your path)

On Jan 24, 2008 11:22 AM, Brandon Rich <brich at caseta.com> wrote:

>

> I have a file named violation that contains both paragraph and image

> tags. While trying to pass these to a PDF, I can get the <para> to pass

> and display properly but not the <image>. I think this is because the

> tags themselves are being passed with the .jpg and as a result, I keep

> getting an error. Can someone please give insight as to what they may

> think the problem is, and any ideas how to get around it?

>

> Thanks

> B

>

> file = open("violation.tmpl", "r")

> tree = parse(file)

> elem = tree.getroot()

> for para in elem.findall('para'):

> contents.append(Paragraph(ElementTree.tostring(para),

> paragraphStyle, None))

> for image in elem.findall('image'):

> contents.append(Image(ElementTree.tostring(image)))

>

> Traceback (most recent call last):

> File "C:\workspace\pythonlibs\tests\document\pdfUtilTest.py", line 6,

> in <module>

> test_letter2(letters_file)

> File "C:\workspace\pythonlibs\tests\document\pdfutil.py", line 117, in

> test_letter2

> pdfDoc.violationNotice(output_file)

> File "C:\workspace\pythonlibs\tests\document\pdfutil.py", line 105, in

> violationNotice

> doc.build( contents, onFirstPage=self.firstPage,

> onLaterPages=self.otherPages )

> File

> "C:\Python25\lib\site-packages\reportlab\platypus\doctemplate.py", line

> 911, in build

> BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)

> File

> "C:\Python25\lib\site-packages\reportlab\platypus\doctemplate.py", line

> 740, in build

> self.handle_flowable(flowables)

> File

> "C:\Python25\lib\site-packages\reportlab\platypus\doctemplate.py", line

> 638, in handle_flowable

> if frame.add(f, self.canv, trySplit=self.allowSplitting):

> File "C:\Python25\lib\site-packages\reportlab\platypus\frames.py",

> line 141, in _add

> w, h = flowable.wrap(aW, h)

> File "C:\Python25\lib\site-packages\reportlab\platypus\flowables.py",

> line 373, in wrap

> return (self.drawWidth, self.drawHeight)

> File "C:\Python25\lib\site-packages\reportlab\platypus\flowables.py",

> line 367, in __getattr__

> self._setup_inner()

> File "C:\Python25\lib\site-packages\reportlab\platypus\flowables.py",

> line 346, in _setup_inner

> img = self._img

> File "C:\Python25\lib\site-packages\reportlab\platypus\flowables.py",

> line 363, in __getattr__

> self._img = ImageReader(self._file)

> File "C:\Python25\lib\site-packages\reportlab\lib\utils.py", line 549,

> in __init__

> self.fp = open_for_read(fileName,'b')

> File "C:\Python25\lib\site-packages\reportlab\lib\utils.py", line 445,

> in open_for_read

> raise IOError('Cannot open resource "%s"' % name)

> IOError: Cannot open resource "<image>

> C:\workspace\pythonlibs\tests\document\test_img.jpg

> </image>

>

> " fileName=<image>

> C:\workspace\pythonlibs\tests\document\test_img.jpg

> </image>

> _______________________________________________

> reportlab-users mailing list

> reportlab-users at reportlab.com

> http://two.pairlist.net/mailman/listinfo/reportlab-users

>



More information about the reportlab-users mailing list