问题
There plenty of examples on how to use Python to control the LibreOffice text document and spreadsheet but very little documentation on how to use the drawing program. I am trying to figure out how to draw a flowchart or at least some shapes in LibreOffice using Python. I am using Windows 10 and the Python 3.3 that came with LibreOffice 5.
There is a very good example of how to use spreadsheet LibreOffice Python example
In the example the following lines are common if you use the text document, spreadsheet, drawing or other documents.
import socket
import uno
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
model = desktop.getCurrentComponent()
The following code was also in the example to modify the spreadsheet program and it works great. The code puts "Hello World" and a number in the spreadsheet.
cell1 = active_sheet.getCellRangeByName("C4")
cell1.String = "Hello world"
cell2 = active_sheet.getCellRangeByName("E6")
cell2.Value = cell2.Value + 1
For the drawing program is there some similar commands to get an active sheet and get a list of shapes that can be drawn? I may be looking in the wrong place but haven't found any documentation for the drawing program.
回答1:
Here is a working Python example:
import uno
def create_shape(document, x, y, width, height, shapeType):
shape = document.createInstance(shapeType)
aPoint = uno.createUnoStruct("com.sun.star.awt.Point")
aPoint.X, aPoint.Y = x, y
aSize = uno.createUnoStruct("com.sun.star.awt.Size")
aSize.Width, aSize.Height = width, height
shape.setPosition(aPoint)
shape.setSize(aSize)
return shape
def insert_shape():
document = XSCRIPTCONTEXT.getDocument()
drawPage = document.getDrawPages().getByIndex(0)
shape = create_shape(
document, 0, 0, 10000, 5000, "com.sun.star.drawing.RectangleShape")
drawPage.add(shape)
shape.setString("My new RectangleShape");
shape.setPropertyValue("CornerRadius", 1000)
shape.setPropertyValue("Shadow", True)
shape.setPropertyValue("ShadowXDistance", 250)
shape.setPropertyValue("ShadowYDistance", 250)
shape.setPropertyValue("FillColor", int("C0C0C0", 16)) # blue
shape.setPropertyValue("LineColor", int("000000", 16)) # black
shape.setPropertyValue("Name", "Rounded Gray Rectangle")
# Functions that can be called from Tools -> Macros -> Run Macro.
g_exportedScripts = insert_shape,
There is fairly complete reference documentation at https://wiki.openoffice.org/wiki/Documentation/DevGuide/Drawings/Working_with_Drawing_Documents. Look especially under the "Shapes" pages (note the navigation on the right-hand side of the page). For one thing, there is a page that gives a list of Shape Types, as you requested.
Since Python-UNO documentation is somewhat limited, you'll need to get used to reading examples in Java or Basic and adapting the code to Python, as I have done above.
来源:https://stackoverflow.com/questions/36852645/create-flowchart-in-libreoffice-using-python