Back to index

plone3  3.1.7
office_uno.py
Go to the documentation of this file.
00001 import re, os, tempfile
00002 import uno
00003 import unohelper
00004 from com.sun.star.beans import PropertyValue
00005 
00006 from Products.PortalTransforms.libtransforms.commandtransform import commandtransform
00007 from Products.PortalTransforms.libtransforms.utils import bodyfinder, scrubHTML
00008 
00009 class document(commandtransform):
00010 
00011     def __init__(self, name, data):
00012         """ Initialization: create tmp work directory and copy the
00013         document into a file"""
00014  
00015         commandtransform.__init__(self, name)
00016         name = self.name()
00017         self.tmpdir, self.fullname = self.initialize_tmpdir(data, filename=name)
00018         self.outputfile = self.fullname + '.html'
00019 
00020     def convert(self):
00021         """Convert the document"""
00022 
00023         localContext = uno.getComponentContext()
00024         resolver = localContext.ServiceManager.createInstanceWithContext(
00025                        'com.sun.star.bridge.UnoUrlResolver', localContext )
00026         ctx = resolver.resolve(
00027                        'uno:socket,host=localhost,port=2002;'
00028                        'urp;StarOffice.ComponentContext')
00029         smgr = ctx.ServiceManager
00030         desktop = smgr.createInstanceWithContext(
00031                        'com.sun.star.frame.Desktop', ctx)
00032 
00033         # load the document
00034         url = unohelper.systemPathToFileUrl(self.fullname)
00035         doc = desktop.loadComponentFromURL(url, '_blank', 0, ())
00036 
00037         filterName = 'swriter: HTML (StarWriter)'
00038         storeProps = (
00039                        PropertyValue('FilterName', 0, filterName, 0),
00040                       )
00041 
00042         # pre-create a empty file for security reason
00043         url = unohelper.systemPathToFileUrl(self.outputfile)
00044         doc.storeToURL(url, storeProps)
00045 
00046         try:
00047             doc.close(True)
00048         except com.sun.star.util.CloseVetoException:
00049             pass
00050 
00051         # maigic to release some resource
00052         ctx.ServiceManager
00053 
00054     def html(self):
00055         htmlfile = open(self.outputfile, 'r')
00056         html = htmlfile.read()
00057         htmlfile.close()
00058         html = scrubHTML(html)
00059         body = bodyfinder(html)
00060         return body