skip to Main Content

I’m currently trying to insert HTML into a LibreOffice Calc Cell.

So far, I’ve tried something like:

<b>Hello</b> <i>world</i>

But currently this is rendered as plain text instead of rich html text.

Is there any way to solve this and possibly add an image too?

2

Answers


  1. LibreOffice formulas do not use HTML syntax.

    However, you can select "Hello" and press Ctrl+B to make it bold, then select "world" and press Ctrl+I for italics. To insert an image, go to Insert > Image.

    Substitution and formatting could also be done with a macro, for example, use a regex to find <b>...</b>, strip the markers, and change that TextPortion formatting to bold.

    Another thing you can do is copy from a webpage and paste HTML formatting with Ctrl+Shift+V. Calc will do its best to convert into LibreOffice formatting, including hyperlinks.

    Login or Signup to reply.
  2. A workaround is to load your HTML into a temporary writer doc and then copy its content to the target calc cell. This works with basic formatting, but not with images, though.

    def InsertHtml2Calc(sHtml, calcDoc, cell):
        oStream = ctx.ServiceManager.createInstanceWithContext("com.sun.star.io.SequenceInputStream", ctx)
        oStream.initialize((uno.ByteSequence(sHtml.encode()),))
    
        prop1 = PropertyValue()
        prop1.Name  = "FilterName"
        prop1.Value = "HTML (StarWriter)"
        prop2 = PropertyValue()
        prop2.Name = "InputStream" 
        prop2.Value = oStream
        prop3 = PropertyValue()
        prop3.Name = "Hidden" 
        prop3.Value = True    
        tmpWriterDoc = desktop.loadComponentFromURL("private:stream", "_default", 0, (prop1, prop2, prop3)) 
    
        tmpWriterContr = tmpWriterDoc.CurrentController 
        oVC = tmpWriterContr.ViewCursor
        oVC.gotoStart(False)
        oVC.gotoEnd(True)
    
        calcDoc.CurrentController.select(cell)
        calcDoc.CurrentController.insertTransferable(tmpWriterContr.Transferable)
        tmpWriterDoc.close(True)
    
    # doc contains a calc doc
    InsertHtml2Calc('Hello <b>bold</b> <i>italics</i><br>2nd line', doc, doc.Sheets.getByIndex(0).getCellByPosition(5,5))
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search