0

I've got the following Python code, running in Django.

from django.http import HttpResponse
from django.shortcuts import render
from django.template import RequestContext, loader
from pptx import Presentation
import uuid
from lxml import etree, objectify
from xml.sax.saxutils import escape

def make(request):
        information = '''<rss version="0.92"><channel><title>JIRA</title><description>An XML representation of a search request</description><language>en-us</language><issue start="0" end="6" total="6"/><build-info><version>6.1-OD-09-WN</version><build-number>6144</build-number><build-date>15-09-2013</build-date></build-info><item><summary>Purchase order submitted to Dell</summary><component>Summer Scaling</component></item></channel></rss>'''
        tree = objectify.fromstring(information)

        components = {}

        for item in tree.channel.item:
                component = str(item.component)
                summary = str(item.summary).strip()
                if component not in components:
                        components[component] = []
                components[component].append(summary)

        prs = Presentation()

        slidelayout = prs.slidelayouts[1]
        for component, summaries in components.iteritems():
                slide = prs.slides.add_slide(slidelayout)
                title = slide.shapes.title
                title.text = str(component)
                body = slide.shapes.placeholders[1]
                first = True
                for summary in summaries:
                        if first:
                                body.textframe.text = summary
                                first = False
                        else:   
                                body.textframe.add_paragraph().text = summary

        filename = '/home/ubuntu/src/jiraxmlapp/download/' + str(uuid.uuid1()) + 'update.pptx'
        prs.save(filename)

        f = open(filename, 'r')
        ppt_content = f.read()

        response = HttpResponse(ppt_content, content_type="application/vnd.openxmlformats-officedocument.presentationml.slideshow")
        response['Content-Disposition'] = 'attachment; filename="status.pptx"'
        return response;

When it executes, I get the following errors.

Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/ubuntu/src/jiraxmlapp/pptmaker/views.py" in make
  17.   tree = objectify.fromstring(information)

Exception Type: XMLSyntaxError at /pptmaker/pptmaker/make/
Exception Value: None

I have tested the syntax of the XML on w3school's website, and it says there are no syntax errors. I've tried formatting the XML string differently ... all on one line, spread across multiple lines, etc. The same error continues to be raised.

To make things more confusing, when I run the first two lines (information = .... and tree = ....) from the shell interpreter, everything works.

I am ready to pull my hair out. Any help would be so very appreciated.

4

0 回答 0