2016-04-25 88 views
2

有很多关于如何使用Python来控制LibreOffice文本文档和电子表格的例子,但关于如何使用绘图程序的文档很少。我想弄清楚如何在使用Python的LibreOffice中绘制流程图或至少某些形状。我使用的是Windows 10以及与LibreOffice的5来到了Python 3.3使用Python在LibreOffice中创建流程图

还有就是如何使用电子表格LibreOffice Python example

在这个例子中,如果您使用的文本文档以下行是常见的一个很好的例子,电子表格,绘图或其他文件。

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() 

下面的代码也是在修改电子表格程序的例子,它的伟大工程。该代码在电子表格中放入了“Hello World”和一个数字。

cell1 = active_sheet.getCellRangeByName("C4") 
cell1.String = "Hello world" 
cell2 = active_sheet.getCellRangeByName("E6") 
cell2.Value = cell2.Value + 1 

对于绘图程序是否有一些类似的命令来获取活动工作表并获取可以绘制的形状列表?我可能在错误的地方看,但没有找到绘图程序的任何文档。

回答

2

这里是一个工作Python的例子:

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, 

有一个在https://wiki.openoffice.org/wiki/Documentation/DevGuide/Drawings/Working_with_Drawing_Documents相当完整的参考文档。特别是在“形状”页面下面(请注意页面右侧的导航)。首先,根据您的要求,有一个页面提供了形状类型列表。

由于Python-UNO文档有一定的限制,您需要习惯于阅读Java或Basic中的示例,并将代码调整为Python,如上所述。