LibreOffice 使用 Python 在形状中绘制哈希标记时出现问题
LibreOffice problems drawing hash marks in shape using Python
我正在尝试使用 LibreOffice 5 在 Windows 10 上使用 LibreOffice 随附的 Python 3.3 在形状内创建哈希标记图案。三分之二的代码与此类似 ,但在代码清单的末尾有关于创建散列标记的其他问题。
这是我试过的 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,
HatchStyle = uno.createUnoStruct("com.sun.star.drawing.HatchStyle")
这失败了,因为 HatchStyle is an Enum, not a Struct。要使用 HatchStyle 枚举,请遵循枚举 link.
中 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)
我正在尝试使用 LibreOffice 5 在 Windows 10 上使用 LibreOffice 随附的 Python 3.3 在形状内创建哈希标记图案。三分之二的代码与此类似
这是我试过的 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,
HatchStyle = uno.createUnoStruct("com.sun.star.drawing.HatchStyle")
这失败了,因为 HatchStyle is an Enum, not a Struct。要使用 HatchStyle 枚举,请遵循枚举 link.
中 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)