Hi all!
Merry Christmas!
Following example has worked until half a year ago.
Now it fails at “cursor.add(oShape)”.
The code draws a line in writer and saves the document.
Note: I am aware that there are some other ways to invoke and control LO.
I was like if it ain’t broke don’t try to fix it.
Best regards,
dr488
#!/bin/python
# -*- coding: utf-8 -*-
# coding: utf8
def rgb(r, g, b):
return 256*256*r + 256*g + b
import sys
import os
import time
import codecs
from subprocess import call
import xml.etree.ElementTree as et
import argparse
import uno
import unohelper
from com.sun.star.beans import PropertyValue
from com.sun.star.awt import Size
from com.sun.star.awt import Point
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", localContext )
smgr = None
while smgr is None:
try:
os.system("libreoffice --headless --accept='socket,host=localhost,port=2002,tcpNoDelay=1;urp' --nodefault --nofirststartwizard --nolockcheck --nologo --norestore --invisible &")
time.sleep(1)
smgr = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" )
except:
smgr = None
os.system("killall soffice.bin 2>/dev/null &")
remoteContext = smgr.getPropertyValue( "DefaultContext" )
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",remoteContext)
dispatcher = smgr.createInstanceWithContext("com.sun.star.frame.DispatchHelper", remoteContext)
doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )
text = doc.Text
cursor = text.createTextCursor()
cLine = doc.createInstance( "com.sun.star.drawing.LineShape" )
ppbz_s = uno.createUnoStruct("com.sun.star.drawing.PolyPolygonBezierCoords")
ppbz_e = uno.createUnoStruct("com.sun.star.drawing.PolyPolygonBezierCoords")
oShape = doc.createInstance( "com.sun.star.drawing.PolyPolygonShape" )
oShape.LineStyle = 1
oShape.LineWidth = 200
oShape.LineColor = rgb(0,0,0)
pts = []
p1 = uno.createUnoStruct("com.sun.star.awt.Point")
p1.X = 1000
p1.Y = 1000
p2 = uno.createUnoStruct("com.sun.star.awt.Point")
p1.X = 11000
p1.Y = 11000
pts.append(p1)
pts.append(p2)
ppbz_s.Coordinates = [pts]
ppbz_s.Flags = []
ppbz_e.Coordinates = [pts]
ppbz_e.Flags = []
oShape.LineStart = ppbz_s
oShape.LineEnd = ppbz_e
oPage = doc.DrawPages[0]
cursor.add(oShape)
properties = (PropertyValue("FilterName", 0, "writer8", 0), PropertyValue("Overwrite", 0, True, 0),)
doc.storeToURL('file:///tmp/test.odt', properties )
properties = (PropertyValue("FilterName", 0, "writer_pdf_Export", 0), PropertyValue("Overwrite", 0, True, 0),)
doc.storeToURL('file:///tmp/test.pdf', properties )
# close the document
dispatcher.executeDispatch(doc.CurrentController.Frame, ".uno:Quit", "", 0, ())