@sokol92, you are right. And the code gets a bit shorter. Now in python:
import uno
from subprocess import Popen
from com.sun.star.beans import PropertyValue # type:ignore
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
lo_proc = Popen('"C:\Program Files\LibreOffice\program\soffice.exe" -accept=socket,host=localhost,port=2002;urp;', shell=True)
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
doc = desktop.loadComponentFromURL('private:factory/swriter', '_default', 0, ())
sHtml = """<P></P>
<P>Paragraph1</P>
Hello from ms777
<b>Bold</b>
<H1>Header 1</H1>
<P>Paragraph2</P>
<CENTER>Center</CENTER>
<TABLE>
<TR><TD bgcolor=#336699>Cell 1</TD><TD bgcolor=#663399>Cell 2</TD></TR>
<TR><TD bgcolor=#123456>the third cell</TD><TD bgcolor=#654321>Cell 4</TD></TR>
</TABLE>"""
def xray(target):
global ctx
mspf = ctx.ServiceManager.createInstanceWithContext("com.sun.star.script.provider.MasterScriptProviderFactory", ctx)
script_provider = mspf.createScriptProvider("")
script = script_provider.getScript("vnd.sun.star.script:XrayTool._Main.Xray?language=Basic&location=application")
script.invoke((target,), (), ())
def InsertHtml2Odt_1(sHTML, doc):
#create a temporary storage
oStorageFac = ctx.ServiceManager.createInstanceWithContext("com.sun.star.embed.StorageFactory", ctx)
oStorage = oStorageFac.createInstance()
oStream = oStorage.openStreamElement("ms777", uno.getConstantByName("com.sun.star.embed.ElementModes.READWRITE"))
#now write the HTML String to the stream
oTextOutputStream = ctx.ServiceManager.createInstanceWithContext("com.sun.star.io.TextOutputStream", ctx)
oTextOutputStream.setOutputStream(oStream)
oTextOutputStream.writeString(sHTML)
prop1 = PropertyValue()
prop1.Name = "FilterName"
prop1.Value = "HTML (StarWriter)"
prop2 = PropertyValue()
prop2.Name = "InputStream"
prop2.Value = oStream
prop3 = PropertyValue()
prop3.Name = "Hidden"
prop3.Value = False
oDoc1 = desktop.loadComponentFromURL("private:stream", "_default", 0, (prop1, prop2, prop3))
oContr1 = oDoc1.CurrentController
oVC1 = oContr1.ViewCursor
oVC1.gotoStart(False)
oVC1.gotoEnd(True)
#insert the selection of doc1 into doc
#note that it is not necessary to involve the clipboard
#for copying
doc.CurrentController.insertTransferable(oContr1.Transferable)
oDoc1.close(True)
def InsertHtml2Odt_2(sHTML, doc):
#create a temporary storage
oStorageFac = ctx.ServiceManager.createInstanceWithContext("com.sun.star.embed.StorageFactory", ctx)
oStorage = oStorageFac.createInstance()
oStream = oStorage.openStreamElement("ms777", uno.getConstantByName("com.sun.star.embed.ElementModes.READWRITE"))
#now write the HTML String to the stream
oTextOutputStream = ctx.ServiceManager.createInstanceWithContext("com.sun.star.io.TextOutputStream", ctx)
oTextOutputStream.setOutputStream(oStream)
oTextOutputStream.writeString(sHTML)
prop1 = PropertyValue()
prop1.Name = "FilterName"
prop1.Value = "HTML (StarWriter)"
prop2 = PropertyValue()
prop2.Name = "InputStream"
prop2.Value = oStream
doc.Text.createTextCursor().insertDocumentFromURL("private:stream", (prop1, prop2))
#InsertHtml2Odt_1(sHtml, doc)
InsertHtml2Odt_2(sHtml, doc)