我正在尝试使用LibreOffice附带的Python 3.3在Windows 10上使用LibreOffice 5在形状内创建一个哈希标记模式。三分之二的代码与此post类似,还有其他关于在代码清单末尾创建哈希标记的问题。
这是我尝试过的Python代码。
import sys
print(sys.version)
import socket
import uno
# get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()
# create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
# connect to the running office
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
# get the central desktop object
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
model = desktop.getCurrentComponent()
# Create the shape
def create_shape(document, x, y, width, height, shapeType):
shape = model.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 formatShape(shape):
shape.setPropertyValue("FillColor", int("FFFFFF", 16)) # blue
shape.setPropertyValue("LineColor", int("000000", 16)) # black
aHatch = uno.createUnoStruct("com.sun.star.drawing.Hatch")
#HatchStyle = uno.createUnoStruct("com.sun.star.drawing.HatchStyle")
#aHatch.Style=HatchStyle.DOUBLE;
aHatch.Color=0x00ff00
aHatch.Distance=100
aHatch.Angle=450
shape.setPropertyValue("FillHatch", aHatch)
shape.setPropertyValue("FillStyle", "FillStyle.DOUBLE")
shape = create_shape(model, 0, 0, 10000, 10000, "com.sun.star.drawing.RectangleShape")
formatShape(shape)
drawPage.add(shape)
此代码应在矩形内设置双交叉线图案,但矩形内没有图案显示。
aHatch = uno.createUnoStruct("com.sun.star.drawing.Hatch")
#HatchStyle = uno.createUnoStruct("com.sun.star.drawing.HatchStyle")
#aHatch.Style=HatchStyle.DOUBLE;
aHatch.Color=0x00ff00
aHatch.Distance=100
aHatch.Angle=450
shape.setPropertyValue("FillHatch", aHatch)
shape.setPropertyValue("FillStyle", "FillStyle.DOUBLE")
设置填充样式模式的行:
uno.RuntimeException: pyuno.getClass:
因以下错误而失败
com.sun.star.drawing.HatchStyleis a ENUM, expected EXCEPTION,
答案 0 :(得分:0)
HatchStyle = uno.createUnoStruct(“com.sun.star.drawing.HatchStyle”)
此操作失败,因为HatchStyle是Enum,而不是Struct。要使用HatchStyle枚举,请从Enum链接中按照python示例中的三种方法之一进行操作。
shape.setPropertyValue(“FillStyle”,“FillStyle.DOUBLE”)
看起来你从例子中混淆了“FillStyle.HATCH”和“HatchStyle.DOUBLE”。这就是Python中的代码:
from com.sun.star.drawing.FillStyle import HATCH
shape.setPropertyValue("FillStyle", HATCH)
这似乎也缺失了:
drawPage = model.getDrawPages().getByIndex(0)