使用Python在LibreOffice中创建流程图 [英] Create Flowchart in LibreOffice using Python

查看:671
本文介绍了使用Python在LibreOffice中创建流程图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

该示例中还包含以下代码,用于修改电子表格程序,并且效果很好.该代码在电子表格中放置了"Hello World"和一个数字.

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.

推荐答案

这是一个有效的Python示例:

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,

https://wiki.openoffice.org上,有相当完整的参考文档. /wiki/Documentation/DevGuide/Drawings/Working_with_Drawing_Documents .尤其要在形状"页面下查看(请注意页面右侧的导航).一方面,有一个页面可以根据您的要求提供形状类型"列表.

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.

由于Python-UNO文档在一定程度上受到限制,因此您需要习惯于阅读Java或Basic中的示例,并像上面所做的那样使代码适应Python.

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.

这篇关于使用Python在LibreOffice中创建流程图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆