[Closed] From Basic to Python UNO : export one doc per page

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sam_fr
Posts: 10
Joined: Sun May 12, 2019 9:43 pm

[Closed] From Basic to Python UNO : export one doc per page

Post by sam_fr »

EDIT : I see the problem occurs when a table is at the very begining of a page. The Basic version breaks then too (for some reason I didn't notice it before). The textcursor.gotoRange doesn't break when the page begins with regular lines of text.

I attach a document bad.odt that makes the script fail.

---
Gathering interesting posts in this forum, I now have a Basic Macro that works fine, but I can't port to Python UNO.

I need to split a Writer document in documents of n pages. For example, 1 document of 100 pages gives me 50 documents of 2 pages.

This works fine with a Basic Macro :

Code: Select all

Sub splitDocument()
' number of pages in each destination doc
pNum = 2

odoc = ThisComponent
nbpages = odoc.CurrentController.PageCount

For pFirst = 0 To CInt(nbpages \ pNum)

	tVC = odoc.CurrentController.GetViewCursor()
	tvc.JumpToPage((pFirst * pNum) +1 )
	tVC.JumpToStartOfPage(False)
	hTC = tVC.Text.CreateTextCursorByRange(tVC)
	For k = 1 To pNum
	  tVC.JumpToEndOfPage()
	  tVC.GoRight(1,False)
	Next k
	hTC.GotoRange(tVC.End, True)
	tVC.GotoRange(hTC.End, False)
	tVC.GotoRange(hTC.Start, True)
	otransferable = odoc.currentController.getTransferable
	
    oNewDoc = StarDesktop.loadComponentFromURL( _
        "private:factory/swriter", "_blank", 0, Array() )
   
    oController = oNewDoc.getCurrentController()
    oController.insertTransferable(otransferable)
    
    oNewDoc.storeAsUrl(converttourl("/tmp/" + CStr(pFirst) + ".odt"), Array)
    oNewDoc.dispose()
    
Next pFirst
End Sub
I try to write it in python, without any success :

Code: Select all

#!/usr/bin/python
import uno
from unohelper import systemPathToFileUrl
from com.sun.star.beans import PropertyValue

local_ctx = uno.getComponentContext()
resolver = local_ctx.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local_ctx)
ctx = resolver.resolve("uno:socket,host=localhost,port=42424;urp;StarOffice.ComponentContext")
desktop = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

inprops = PropertyValue("Hidden", 0, True, 0),
doc = desktop.loadComponentFromURL('/tmp/document.odt', "_blank", 0, inprops)
nbpages = doc.CurrentController.PageCount

pagesperdoc = 2
for nopage in range(nbpages // pagesperdoc):

    visiblecursor = doc.CurrentController.getViewCursor()
    visiblecursor.jumpToPage(nopage * pagesperdoc + 1)
    visiblecursor.jumpToStartOfPage()
    htc = visiblecursor.Text.createTextCursorByRange(visiblecursor)

    for p in range(pagesperdoc):
        visiblecursor.jumpToEndOfPage()
        visiblecursor.goRight(1, False)

    # IT BREAKS HERE :
    htc.gotoRange(visiblecursor.End, True)
    
    visiblecursor.gotoRange(htc.End, False)
    visiblecursor.gotoRange(htc.Start, True)

    otransferable = doc.CurrentController.getTransferable()
    blank = "private:factory/swriter"
    newdoc = desktop.loadComponentFromURL(blank, "_blank", 0, inprops)
    newdoc.CurrentController.insertTransferable(otransferable)

    dest = '/tmp/out'
    destodt = systemPathToFileUrl('{}-{}.{}'.format(dest, nopage, "odt"))
    newdoc.storeToURL(destodt, [])
    newdoc.dispose()
The

Code: Select all

htc.gotoRange(visiblecursor.End, True)
ends with :

Code: Select all

Error (<class 'uno.com.sun.star.uno.RuntimeException'>) during conversion:
Despite it works in Basic with the same doc. I'm stuck with this.
Thanks
Attachments
bad.odt
(7.93 KiB) Downloaded 159 times
OpenOffice 4, LibreOffice 6 on Ubuntu 18.04
sam_fr
Posts: 10
Joined: Sun May 12, 2019 9:43 pm

Re: [Closed] From Basic to Python UNO : export one doc per p

Post by sam_fr »

Closed because the problems isn't related to python vs basic. I will open a proper post
OpenOffice 4, LibreOffice 6 on Ubuntu 18.04
Post Reply