コード例 #1
0
ファイル: abiword.py プロジェクト: corentinl/django-webodt
 def convert(self, document, format=None, output_filename=None, delete_on_close=True):
     output_filename, format = guess_format_and_filename(output_filename, format)
     process = subprocess.Popen(WEBODT_ABIWORD_COMMAND,
         stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
     )
     args = (document.name, output_filename, format)
     process.communicate('convert %s %s %s' % args)
     fd = Document(output_filename, mode='r', delete_on_close=delete_on_close)
     return fd
コード例 #2
0
ファイル: openoffice.py プロジェクト: ckarrie/django-webodt
 def convert(self,
             document,
             format=None,
             output_filename=None,
             delete_on_close=True):
     output_filename, format = guess_format_and_filename(
         output_filename, format)
     ### Do the OpenOffice component dance
     context = uno.getComponentContext()
     resolver = context.ServiceManager.createInstanceWithContext(
         'com.sun.star.bridge.UnoUrlResolver', context)
     unocontext = resolver.resolve('uno:%s' % OOO_CONNECTION)
     ### And some more OpenOffice magic
     unosvcmgr = unocontext.ServiceManager
     desktop = unosvcmgr.createInstanceWithContext(
         'com.sun.star.frame.Desktop', unocontext)
     config = unosvcmgr.createInstanceWithContext(
         'com.sun.star.configuration.ConfigurationProvider', unocontext)
     ### Load inputfile
     instream = InputStream(uno.ByteSequence(document.read()))
     inputprops = [
         PropertyValue('InputStream', 0, instream, 0),
     ]
     if document.format == 'html':
         inputprops.append(
             PropertyValue('FilterName', 0, 'HTML (StarWriter)', 0))
     doc = desktop.loadComponentFromURL('private:stream', '_blank', 0,
                                        tuple(inputprops))
     ### Update document links
     # skip ...
     ### Update document indexes
     # skip ...
     ### Write outputfile
     fd = open(output_filename, 'w')
     filter_name = formats[format]
     outputprops = [
         PropertyValue(
             'FilterData', 0,
             uno.Any(
                 '[]com.sun.star.beans.PropertyValue',
                 tuple(),
             ), 0),
         PropertyValue('FilterName', 0, filter_name, 0),
         PropertyValue('OutputStream', 0, OutputStream(fd), 0),
         PropertyValue('Overwrite', 0, True, 0),
     ]
     if filter_name == 'Text (encoded)':
         outputprops.append(PropertyValue('FilterFlags', 0, 'UTF8, LF', 0))
     doc.storeToURL('private:stream', tuple(outputprops))
     doc.dispose()
     doc.close(True)
     fd.close()
     fd = Document(output_filename,
                   mode='r',
                   delete_on_close=delete_on_close)
     return fd
コード例 #3
0
ファイル: googledocs.py プロジェクト: Bochozkar/django-webodt
    def convert(self, document, format=None, output_filename=None, delete_on_close=True):
        # opener = urllib2.build_opener(urllib2.HTTPSHandler(debuglevel=1))
        # urllib2.urlopen = opener.open
        output_filename, format = guess_format_and_filename(output_filename, format)
        # upload document
        url = 'https://docs.google.com/feeds/default/private/full'
        data = document.read()
        headers = {
            'GData-Version': '3.0',
            'Authorization': 'GoogleLogin auth=%s' % self.auth_token,
            'Content-Length': str(len(data)),
            'Content-Type': document.content_type,
            'Slug': '%s.%s' % (uuid.uuid4(), document.format),
        }
        request = urllib2.Request(url, data, headers)
        response = urllib2.urlopen(request)
        data = response.read()
        response.close()
        tree = etree.parse(StringIO(data))
        # get document resource id
        resource_id = tree.xpath('gd:resourceId/text()', namespaces={'gd': 'http://schemas.google.com/g/2005'})
        if len(resource_id) != 1:
            raise ValueError('Unexpected error. Document schema was changed')
        resource_id = resource_id[0]
        # get document URL
        document_url = tree.xpath('atom:content/@src', namespaces={'atom': 'http://www.w3.org/2005/Atom'})
        if len(document_url) != 1:
            raise ValueError('Unexpected error. Document schema was changed')
        document_url = document_url[0]
        url = document_url + '&exportFormat=%(format)s&format=%(format)s' % {'format': format}
        # download document
        headers = {
            'GData-Version': '3.0',
            'Authorization': 'GoogleLogin auth=%s' % self.auth_token,
        }
        request = urllib2.Request(url, None, headers)
        response = urllib2.urlopen(request)
        data = response.read()
        response.close()
        fd = open(output_filename, 'w')
        fd.write(data)
        fd.close()

        # remove document from google docs
        self._remove_document(resource_id)

        # return document
        fd = Document(output_filename, mode='r', delete_on_close=delete_on_close)
        return fd
コード例 #4
0
 def convert(self,
             document,
             format=None,
             output_filename=None,
             delete_on_close=True):
     output_filename, format = guess_format_and_filename(
         output_filename, format)
     process = subprocess.Popen(WEBODT_ABIWORD_COMMAND,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
     args = (document.name, output_filename, format)
     process.communicate('convert %s %s %s' % args)
     fd = Document(output_filename,
                   mode='r',
                   delete_on_close=delete_on_close)
     return fd
コード例 #5
0
    def convert(self, document, format=None, output_filename=None, delete_on_close=True):
        if format != 'pdf':
            raise ConverterError, u'Not supported format %s by xhtml2pdf' % format
        output_filename, format = guess_format_and_filename(output_filename, format)
        input_file = document
        input_filename = document.name
        output_file = open(output_filename, 'wb')

        result = pisa.pisaDocument(
            input_file, output_file, path=input_filename, encoding='UTF-8',
        )

        output_file.close()

        if result.err:
            err_msg = 'Error rendering %s: %s' % (input_filename, result.err)
            raise ConverterError, err_msg
        fd = Document(output_filename, mode='rb', delete_on_close=delete_on_close)
        return fd
コード例 #6
0
ファイル: openoffice.py プロジェクト: hartred/django-webodt
 def convert(self, document, format=None, output_filename=None, delete_on_close=True):
     output_filename, format = guess_format_and_filename(output_filename, format)
     ### Do the OpenOffice component dance
     context = uno.getComponentContext()
     resolver = context.ServiceManager.createInstanceWithContext('com.sun.star.bridge.UnoUrlResolver', context)
     unocontext = resolver.resolve('uno:%s' % OOO_CONNECTION)
     ### And some more OpenOffice magic
     unosvcmgr = unocontext.ServiceManager
     desktop = unosvcmgr.createInstanceWithContext('com.sun.star.frame.Desktop', unocontext)
     config = unosvcmgr.createInstanceWithContext('com.sun.star.configuration.ConfigurationProvider', unocontext)
     ### Load inputfile
     instream = InputStream(uno.ByteSequence(document.read()))
     inputprops = [
         PropertyValue('InputStream', 0, instream, 0),
     ]
     if document.format == 'html':
         inputprops.append(PropertyValue('FilterName', 0, 'HTML (StarWriter)', 0))
     doc = desktop.loadComponentFromURL('private:stream', '_blank', 0, tuple(inputprops))
     ### Update document links
     # skip ...
     ### Update document indexes
     # skip ...
     ### Write outputfile
     fd = open(output_filename, 'w')
     filter_name = formats[format]
     outputprops = [
         PropertyValue('FilterData', 0, uno.Any('[]com.sun.star.beans.PropertyValue', tuple(),), 0),
         PropertyValue('FilterName', 0, filter_name, 0),
         PropertyValue('OutputStream', 0, OutputStream(fd), 0),
         PropertyValue('Overwrite', 0, True, 0),
     ]
     if filter_name == 'Text (encoded)':
         outputprops.append(PropertyValue('FilterFlags', 0, 'UTF8, LF', 0))
     doc.storeToURL('private:stream', tuple(outputprops))
     doc.dispose()
     doc.close(True)
     fd.close()
     fd = Document(output_filename, mode='r', delete_on_close=delete_on_close)
     return fd
コード例 #7
0
    def convert(self,
                document,
                format=None,
                output_filename=None,
                delete_on_close=True):
        # opener = urllib2.build_opener(urllib2.HTTPSHandler(debuglevel=1))
        # urllib2.urlopen = opener.open
        output_filename, format = guess_format_and_filename(
            output_filename, format)
        # upload document
        url = 'https://docs.google.com/feeds/default/private/full'
        data = document.read()
        headers = {
            'GData-Version': '3.0',
            'Authorization': 'GoogleLogin auth=%s' % self.auth_token,
            'Content-Length': str(len(data)),
            'Content-Type': document.content_type,
            'Slug': '%s.%s' % (uuid.uuid4(), document.format),
        }
        request = urllib2.Request(url, data, headers)
        response = urllib2.urlopen(request)
        data = response.read()
        response.close()
        tree = etree.parse(StringIO(data))
        # get document resource id
        resource_id = tree.xpath(
            'gd:resourceId/text()',
            namespaces={'gd': 'http://schemas.google.com/g/2005'})
        if len(resource_id) != 1:
            raise ValueError('Unexpected error. Document schema was changed')
        resource_id = resource_id[0]
        # get document URL
        document_url = tree.xpath(
            'atom:content/@src',
            namespaces={'atom': 'http://www.w3.org/2005/Atom'})
        if len(document_url) != 1:
            raise ValueError('Unexpected error. Document schema was changed')
        document_url = document_url[0]
        url = document_url + '&exportFormat=%(format)s&format=%(format)s' % {
            'format': format
        }
        # download document
        headers = {
            'GData-Version': '3.0',
            'Authorization': 'GoogleLogin auth=%s' % self.auth_token,
        }
        request = urllib2.Request(url, None, headers)
        response = urllib2.urlopen(request)
        data = response.read()
        response.close()
        fd = open(output_filename, 'w')
        fd.write(data)
        fd.close()

        # remove document from google docs
        self._remove_document(resource_id)

        # return document
        fd = Document(output_filename,
                      mode='r',
                      delete_on_close=delete_on_close)
        return fd