使用Python在LibreOffice中创建流程图

使用Python在LibreOffice中创建流程图,python,libreoffice,flowchart,Python,Libreoffice,Flowchart,关于如何使用Python控制LibreOffice文本文档和电子表格的示例很多,但关于如何使用绘图程序的文档却很少。我正在试图弄清楚如何使用Python在LibreOffice中绘制流程图或至少一些形状。我使用的是Windows 10和LibreOffice 5附带的Python 3.3 关于如何使用电子表格,有一个很好的例子 在本例中,如果使用文本文档、电子表格、图形或其他文档,则以下行是常见的 import socket import uno localContext = uno.get

关于如何使用Python控制LibreOffice文本文档和电子表格的示例很多,但关于如何使用绘图程序的文档却很少。我正在试图弄清楚如何使用Python在LibreOffice中绘制流程图或至少一些形状。我使用的是Windows 10和LibreOffice 5附带的Python 3.3

关于如何使用电子表格,有一个很好的例子

在本例中,如果使用文本文档、电子表格、图形或其他文档,则以下行是常见的

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

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

下面是一个运行的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,
网站上有相当完整的参考文档。特别是在“形状”页面下查看(注意页面右侧的导航)。首先,根据您的要求,有一个页面提供了形状类型的列表

由于Python UNO文档有一定的局限性,您需要习惯于阅读Java或Basic中的示例,并将代码改编为Python,正如我前面所做的那样