[Solved] TYPE error using InputStream with transferable data

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

[Solved] TYPE error using InputStream with transferable data

Post by Bastien »

I'm trying to write in Python a BASIC code to copy a text from a document (keeping styles, bold or not) to another. The basic source is here viewtopic.php?f=20&t=56753&p=249848&hil ... le#p249987.
The line that raises a "CannotConvertException" is :

Code: Select all

args = (PropertyValue('InputStream', oIn, "FilterName", "writer_Rich_Text_Format"),)
Here is the complete macro:

Code: Select all

def python_bal(*args):
    """
    Main function launched from toolbar button.
    """

    desktop = XSCRIPTCONTEXT.getDesktop()
    ctx = XSCRIPTCONTEXT.getComponentContext()
    document = XSCRIPTCONTEXT.getDocument()

    # Position a cursor at the very beginning of the doc
    oVC = document.getCurrentController().getViewCursor()
    oVC.jumpToFirstPage()

    # Select text to copy with style (bold or not)
    oVC.gotoEnd(True)

    # Get transferable
    oTransferable = document.getCurrentController().getTransferable()
    dataFlavors = oTransferable.getTransferDataFlavors()

    # Get specific DataFlavor
    sType = "text/richtext"
    for flavor in dataFlavors:
        if flavor.MimeType == sType:
            aMtype = flavor

    if oTransferable.isDataFlavorSupported(aMtype):
        nData = oTransferable.getTransferData(aMtype)

    # Dark part of the trick
    oTemp = createUnoService("com.sun.star.io.TempFile")
    oOut = oTemp.getOutputStream()
    oOut.writeBytes(nData)
    oOut.flush()

    oIn = oTemp.getInputStream()

    # Create a new doc
    args = (PropertyValue('Hidden', 0, False, 0),)
    newDoc = desktop.loadComponentFromURL(
        get_template_url(), "_default", 0, args
        )

    # Position the cursor at the end of the new doc
    oText = newDoc.getText()
    oCursor = oText.createTextCursor()
    oCursor.gotoEnd(False)

    # Insert transferable data at the cursor's position
    args = (PropertyValue('InputStream', oIn, "FilterName", "writer_Rich_Text_Format"),)
    oCursor.insertDocumentFromURL("", args)

    return None
Except this exception, all the macro runs correctly. Text is selected. Transferable is ok. New doc is created. It just fails at the end.
Last edited by Bastien on Tue Mar 05, 2019 6:46 pm, edited 2 times in total.
LibreOffice 6.2.3.2 on Ubuntu 19.04
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: TYPE error using InputStream with transferable data

Post by JeJe »

Have you tried just using inserttransferable?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: TYPE error using InputStream with transferable data

Post by Bastien »

Inserttransferable? What do you mean? It doesn't seem to be a method of textCursor.
LibreOffice 6.2.3.2 on Ubuntu 19.04
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: TYPE error using InputStream with transferable data

Post by JeJe »

newDoc.getCurrentController().insertTransferable( oTransferable)

https://www.openoffice.org/api/docs/com ... plier.html
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: TYPE error using InputStream with transferable data

Post by Bastien »

Thanks man !
Here is the working python code to do the job:

Code: Select all

def python_bal(*args):
    """
    Main function launched from toolbar button.
    """

    desktop = XSCRIPTCONTEXT.getDesktop()
    ctx = XSCRIPTCONTEXT.getComponentContext()
    document = XSCRIPTCONTEXT.getDocument()

    # Position a cursor at the very beginning of the doc
    oVC = document.getCurrentController().getViewCursor()
    oVC.jumpToFirstPage()

    # If cursor is in table, we jump over it
    while oVC.TextTable:
        oVC.goDown(1, False)

    # Select text to copy with style (bold or not)
    oVC.gotoEnd(True)

    # Get transferable
    oTransferable = document.getCurrentController().getTransferable()
    dataFlavors = oTransferable.getTransferDataFlavors()

    # Create a new doc
    args = (PropertyValue('Hidden', 0, False, 0),)
    newDoc = desktop.loadComponentFromURL(
        get_template_url(), "_default", 0, args
        )

    # Insert transferable data at the cursor's position
    oCursor = newDoc.getCurrentController().getViewCursor()
    oCursor.jumpToFirstPage()
    oCursor.jumpToNextPage()
    newDoc.getCurrentController().insertTransferable(oTransferable)

    return None
LibreOffice 6.2.3.2 on Ubuntu 19.04
Post Reply