libreoffice python_使用Python在LibreOffice中创建流程图

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值