使用 Python 在 LibreOffice 中创建流程图
Create Flowchart in LibreOffice using Python
关于如何使用Python控制LibreOffice文本文档和传播的例子很多sheet但是关于如何使用绘图程序的文档很少。我正在尝试弄清楚如何使用 Python 在 LibreOffice 中绘制流程图或至少绘制某些形状。我正在使用 Windows 10 和 LibreOffice 5 附带的 Python 3.3。
有一个很好的例子说明如何使用spreadsheet LibreOffice Python example
在示例中,如果您使用文本文档、展开sheet、绘图或其他文档,则以下行很常见。
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()
下面的代码也在示例中修改了spreadsheet程序,效果很好。该代码将 "Hello World" 和一个数字放在价差 sheet.
中
cell1 = active_sheet.getCellRangeByName("C4")
cell1.String = "Hello world"
cell2 = active_sheet.getCellRangeByName("E6")
cell2.Value = cell2.Value + 1
对于绘图程序,是否有一些类似的命令来激活 sheet 并获取可绘制的形状列表?我可能找错了地方,但没有找到绘图程序的任何文档。
这是一个有效的 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 上有相当完整的参考文档。尤其要查看 "Shapes" 页面下方(请注意页面右侧的导航)。一方面,根据您的要求,有一个页面提供了形状类型列表。
由于 Python-UNO 文档有些限制,您需要习惯阅读 Java 或 Basic 中的示例并将代码调整为 Python,就像我一样以上完成。
关于如何使用Python控制LibreOffice文本文档和传播的例子很多sheet但是关于如何使用绘图程序的文档很少。我正在尝试弄清楚如何使用 Python 在 LibreOffice 中绘制流程图或至少绘制某些形状。我正在使用 Windows 10 和 LibreOffice 5 附带的 Python 3.3。
有一个很好的例子说明如何使用spreadsheet LibreOffice Python example
在示例中,如果您使用文本文档、展开sheet、绘图或其他文档,则以下行很常见。
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()
下面的代码也在示例中修改了spreadsheet程序,效果很好。该代码将 "Hello World" 和一个数字放在价差 sheet.
中cell1 = active_sheet.getCellRangeByName("C4")
cell1.String = "Hello world"
cell2 = active_sheet.getCellRangeByName("E6")
cell2.Value = cell2.Value + 1
对于绘图程序,是否有一些类似的命令来激活 sheet 并获取可绘制的形状列表?我可能找错了地方,但没有找到绘图程序的任何文档。
这是一个有效的 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 上有相当完整的参考文档。尤其要查看 "Shapes" 页面下方(请注意页面右侧的导航)。一方面,根据您的要求,有一个页面提供了形状类型列表。
由于 Python-UNO 文档有些限制,您需要习惯阅读 Java 或 Basic 中的示例并将代码调整为 Python,就像我一样以上完成。