【问题标题】:Create Flowchart in LibreOffice using Python使用 Python 在 LibreOffice 中创建流程图
【发布时间】:2016-04-25 23:08:17
【问题描述】:

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

有一个很好的例子说明如何使用电子表格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

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

【问题讨论】:

    标签: python libreoffice flowchart


    【解决方案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,
    

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

    由于 Python-UNO 文档有些有限,您需要习惯于阅读 Java 或 Basic 中的示例并将代码改编为 Python,就像我在上面所做的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-12
      • 2022-08-05
      • 2018-09-18
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      相关资源
      最近更新 更多