Ask Your Question
1

Basic Macro to export specific pages of a writer document

asked 2018-01-28 22:18:17 +0200

daniel1 gravatar image

updated 2018-01-28 22:18:58 +0200

Hey,

can anyone tell me how to export specific pages of a writer document to a xml-file with a basic macro? The goal is to get a xml-file like that:

<pages>
 <page>
 Text of page 1
 <\page>
 <page>
 Text of page 2
 <\page>
<\pages>
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2018-01-29 06:40:25 +0200

librebel gravatar image

Hello @daniel1,

To export specific pages from the current Writer document to an xml-file , according to the stated goal structure, you could use the following Basic macro:

Sub Writer_Export_Pages_To_XML( strFile As String, Optional iStartPage, Optional iEndPage )
REM Export Pages from the current Writer document as XML structure.
REM See https://ask.libreoffice.org/en/question/144315/basic-macro-to-export-specific-pages-of-a-writer-document/
REM Based upon Original Code by ( Sébastien C ) here: https://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=86044
REM <strFile>   : Path to the XML-file into which the pages will be exported ( e.g. "/home/user/Desktop/myPages.xml" );
REM               This file will be created if non-existent, and will be overwritten if it already exists.
REM <iStartPage>: Integer specifying the first page to Export;  DEFAULT=1.
REM <iEndPage>  : Integer specifying the last page to Export;   DEFAULT=<Total number of Pages>.
    Dim oDoc As Object : oDoc = ThisComponent
    If oDoc.supportsService( "com.sun.star.text.TextDocument" ) Then

        Dim oDocBuilder As Object : oDocBuilder = createUnoService( "com.sun.star.xml.dom.DocumentBuilder" )
        Dim oXML As Object        : oXML = oDocBuilder.newDocument()

        Dim oPages As Object      : oPages = oXML.createElement( "pages" )  REM Root Node = "<pages>".
        oXML.appendChild( oPages )

        Dim iPageCount As Integer   REM Get the number of pages in the current Writer document:
        Dim oCursor As Object : oCursor = oDoc.CurrentController.getViewCursor()
        If oCursor.jumpToLastPage() Then iPageCount = oCursor.getPage()

        If IsMissing( iStartPage ) Then iStartPage = 1
        If IsMissing( iEndPage ) Then iEndPage = iPageCount
        If iEndPage > iPageCount Then iEndPage = iPageCount

        Dim oPage As Object, strPage As String  REM Add a Child Node for each Page:
        Dim oLeaf As Object, oStart As Object, i As Integer
        For i = iStartPage To iEndPage

            oCursor.gotoStart( False )      REM Unselect the previous selection.
            If oCursor.jumpToPage( i ) Then     REM Get the text of page <i>.
                oStart = oCursor.getStart()
                oCursor.jumpToEndOfPage()
                oCursor.gotoRange( oStart, True )
                strPage = oCursor.getString()
            End If

            oPage = oXML.createElement( "page" )
            oLeaf = oXML.createTextNode( strPage )
            oPage.appendChild( oLeaf )
            oPages.appendChild( oPage )
        Next

        If strFile <> "" Then   REM Export the XML structure to file:
            Dim oFileAccess As Object : oFileAccess = createUNOService( "com.sun.star.ucb.SimpleFileAccess" )
            If oFileAccess.exists( strFile ) Then oFileAccess.kill( strFile )       REM Overwrite.
            Dim oStream As Object : oStream = oFileAccess.openFileWrite( ConvertToURL( strFile ) )
            oXML.setOutputStream( oStream )
            oXML.start()
            oStream.closeOutput()
        End If

    End If
End Sub

HTH, With Regards,

lib


To accept this answer, please click on the checkmark icon on the left so that it turns green.

edit flag offensive delete link more

Comments

Thank you for your answer! For testing i tried to execute the macro with this Sub:

Sub Test
Writer_Export_Pages_To_XML("/test.xml")
End Sub

If i do this, i get a runtime error "Type: com.sun.star.ucb.InteractiveAugmentedIOException Message: an error occurred during file opening."

daniel1 gravatar imagedaniel1 ( 2018-01-29 23:26:07 +0200 )edit

it must be a full path, e.g. "/home/user/Desktop/test.xml"

( NB. Windows path uses the other slashes "\" )

librebel gravatar imagelibrebel ( 2018-01-29 23:33:01 +0200 )edit

Oh, i see, thank you. Another question: How can i get it to export line breaks?

daniel1 gravatar imagedaniel1 ( 2018-01-29 23:46:49 +0200 )edit

Linebreak characters typed explicitly into the text by pressing the “ENTER”-key, are exported.

But Automatic Linebreaks caused by Wrapping the text inside the current frame width, are not exported, since these are not part of the text.

librebel gravatar imagelibrebel ( 2018-01-30 00:16:46 +0200 )edit

There is a way to insert explicit linebreak characters into the exported text at the end of each visible line in the original text, but then the exported text would be different from the original text.

If you still want to do that, then please make it into another Question, and mark this Answer as correct by clicking on the Checkmark icon on the left.

librebel gravatar imagelibrebel ( 2018-01-30 00:18:33 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-01-28 22:18:17 +0200

Seen: 199 times

Last updated: Jan 29 '18