Hi,

I wrote this little python script below to split up a document into
fragments to reply to a mail on the UDK list to show how to copy part of
a document but I think it would be more useful on the this list (API)

It fragments the document into 30 lines pieces, and copies the fragment
into a blank document. This will copy almost every thing in the 30 lines
including text, tables and graphics.

Regards,
-- 
Darragh Sherwin
PWB Implementations Manager
http://www.propylon.com/solutions/parliament/index.html
+353-1-4927456
+353-87-1204654

======================================================================

import unohelper
from com.sun.star.task import XJobExecutor
from com.sun.star.beans import PropertyValue

"""
Get all the required services for the current open document
"""
def getDesktopDict( ctx ):

        desktop = ctx.ServiceManager.createInstanceWithContext( \
            "com.sun.star.frame.Desktop", ctx )

        #Get the current controller and viewCursor

        document = desktop.getCurrentComponent()
        controller = document.getCurrentController()
        viewCursor = controller.getViewCursor()

        document.RecordChanges = False

        return { 'desktop'    : desktop,
                 'document'  : document,
                 'controller' : controller,
                 'viewcursor' : viewCursor }


"""
Create a new, blank document and get all the specific services required
"""
def createNewDocument( deskDict, url="private:factory/swriter" ):

        """
        loadProps = uno.createUnoStruct(
"com.sun.star.beans.PropertyValue" )
        loadProps.Name = "Hidden"
        loadProps.Value = True
        """

        newDocument = deskDict['desktop'].loadComponentFromURL( \
                        url, "_blank", 0, () )
        controller = newDocument.getCurrentController()
        viewCursor = controller.getViewCursor()

        return  { 'document' : newDocument,
                'controller' : controller,
                'viewcursor' : viewCursor,
                'desktop'    : deskDict['desktop']
                }


"""
A wrapper function to wrap around OpenOffice's
Dispatch API.
islot is a Slot ID ( aka Command Name )
For all Slot IDs see
http://framework.openoffice.org/servlets/ProjectDocumentList?folderID=72&expandFolder=72&folderID=71
"""
def executeSlot( ctx, controller, islot ):

        dispatchHelper = ctx.ServiceManager.createInstanceWithContext( \
                "com.sun.star.frame.DispatchHelper", ctx )
        frame = controller.getFrame()

        dispatchHelper.executeDispatch( frame, islot, "", 0, () )


"""
Connection Details to connect
over an InterProcess Communication (IPC)
pipe to the running OpenOffice process
"""
def connect():
        import uno
        localContext = uno.getComponentContext()
        resolver = localContext.ServiceManager.createInstanceWithContext( \
            "com.sun.star.bridge.UnoUrlResolver",  localContext )
        remoteContext = resolver.resolve( \

"uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
        return remoteContext


"""
Class to copy 30 lines of text
This will break table up, if there is a table
in the 30 lines selected by the viewCursor
"""
class Copier( unohelper.Base, XJobExecutor ):

  def __init__( self, ctx ):

        self.ctx = ctx
        deskDict = getDesktopDict( ctx )
        self.splitDocument( deskDict )

  """
  Splits the document
  """
  def splitDocument( self, deskDict ):

        viewCursor = deskDict['viewcursor']
        text = deskDict['document'].Text
        controller = deskDict['controller']
        textCursor = text.createTextCursor()
        textCursor.gotoRange( text.getStart(), False )

        viewCursor.gotoRange( deskDict['document'].Text.getStart(), False )

        deskDict['viewcursor'] = viewCursor

        endRange = text.getEnd()

        viewCursor.gotoStart( False )
        while ( text.compareRegionEnds( viewCursor, endRange ) > 0 ):

                """
                The viewCursor is selecting the 30 lines
                """
                viewCursor.goDown( 30, True )
                controller.select( controller.getSelection() )

                """
                This executes the copy function
                """
                executeSlot( self.ctx, controller,  ".uno:Copy")

                docDict = createNewDocument( deskDict )
                self.pasteToNewDoc( docDict )
                controller.getFrame().getComponentWindow().setFocus( )
                textCursor.gotoRange( viewCursor.getEnd() , False)
                viewCursor.gotoRange( textCursor, False )


  """
  Pastes the system clipboard into the document
  """
  def pasteToNewDoc( self, deskDict ):

        document = deskDict['document']
        executeSlot( self.ctx, deskDict['controller'], ".uno:Paste")


if __name__=="__main__":

        remoteContext = connect()
        try:
                job = Copier( remoteContext )
        except Exception, e:
                print "\n\n"
                import traceback
                traceback.print_exc()

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




-- 
Darragh Sherwin
PWB Implementations Manager
http://www.propylon.com/solutions/parliament/index.html
+353-1-4927456
+353-87-1204654

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to