我的任务是为 LibreOffice(准确地说是 Collabora online)编写 python 脚本,该脚本将用户字段插入到文档中。我在 LibreOffice 中记录了宏以及所需的操作
sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
dim args1(5) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Type"
args1(0).Value = 20
args1(1).Name = "SubType"
args1(1).Value = 1
args1(2).Name = "Name"
args1(2).Value = "FIO"
args1(3).Name = "Content"
args1(3).Value = "FIO222"
args1(4).Name = "Format"
args1(4).Value = -1
args1(5).Name = "Separator"
args1(5).Value = " "
dispatcher.executeDispatch(document, ".uno:InsertField", "", 0, args1())
End sub
将这段代码重写为python,但没有成功。
import uno
def InsertLinkedImage():
ctx = XSCRIPTCONTEXT.getComponentContext()
smgr = ctx.ServiceManager
oDisp = smgr.createInstanceWithContext(
"com.sun.star.frame.DispatchHelper", ctx)
oDoc = XSCRIPTCONTEXT.getDocument()
oFrame = oDoc.getCurrentController().getFrame()
props = (
createProp("Type", 20),
createProp("SubType", 1),
createProp("Name", "FIO"),
createProp("Content", "FIO222"),
createProp("Format", -1),
createProp("Separator", ""),
)
oDisp.executeDispatch(oFrame, ".uno:InsertField", "", 0, props)
def createProp(name, value):
"""Creates an UNO property."""
prop = uno.createUnoStruct("com.sun.star.beans.PropertyValue")
prop.Name = name
prop.Value = value
return prop
我在这里找到了具有相同功能的脚本(可能是 Starbasic),但我不知道如何在 python 中使用它来达到我的目的。
Sub InsertFieldIntoDocument(sFieldName As String, Optional bSection As Boolean, Optional bSectionOpened As Boolean)
Dim oDoc As Object
Dim oText As Object
Dim oViewCursor As Object
Dim oUserFieldMaster, oUserFieldMasters, oUserField As Object
Dim bMasterExists As Boolean
Dim sPrefix As String
oDoc = ThisComponent
oText = oDoc.getText()
oViewCursor = oDoc.getCurrentController().getViewCursor()
' Handling optional values
If IsMissing(bSection) Then bSection = False
If IsMissing(bSectionOpened) Then bSectionOpened = False
' Getting the collection of masters of user fields
oUserFieldMasters = oDoc.getTextFieldMasters()
bMasterExists = False
' Checks is master field already exists
sCalculatedFieldName = sFieldName
' If it's a section, we have to search for a specific field name
If bSection Then
If bSectionOpened Then
sCalculatedFieldName = "SECT_" & sFieldName
Else
sCalculatedFieldName = "SECT_END_" & sFieldName
End If
End If
If oUserFieldMasters.hasByName("com.sun.star.text.fieldmaster.User." & sCalculatedFieldName) Then
bMasterExists = True
oUserFieldMaster = oUserFieldMasters.getByName("com.sun.star.text.fieldmaster.User." & sCalculatedFieldName)
End If
' Creates master field if it doesn't exist
If Not bMasterExists Then
oUserFieldMaster = oDoc.createInstance("com.sun.star.text.fieldmaster.User")
oUserFieldMaster.Name = sCalculatedFieldName
oUserFieldMaster.Content = ""
Msgbox oUserFieldMaster.Value
End If
' Creates the textfield that uses master
oUserField = oDoc.createInstance("com.sun.star.text.textfield.User")
oUserField.attachTextFieldMaster(oUserFieldMaster)
' Insert the field in the document
oText.insertTextContent(oViewCursor, oUserField, False)
End Sub
我已经在网上搜索了几天,但没有结果。
这不是答案,但这是我不久前编写的一些代码,您可能会发现它们对尝试解决问题很有帮助。
oDoc = XSCRIPTCONTEXT.getDocument()
xTextFieldsSupplier = oDoc
if not xTextFieldsSupplier:
raise Exception("No xTextFieldsSupplier")
xTextFieldsInfo = xTextFieldsSupplier.getTextFieldMasters()
if not xTextFieldsInfo:
raise Exception("No xTextFieldsInfo")
xTextFieldsNames = xTextFieldsInfo.getElementNames()
oVC = oDoc.getCurrentController().getViewCursor()
oVC.getText().insertString(oVC, "Hello!\n", False)
for field in xTextFieldsNames:
xField = xTextFieldsInfo.getByName(field)
oVC.getText().insertString(oVC, "{}\n".format(xField.Name), False)
另外,查看我的其他一些代码,似乎我也使用了调度程序。
uno_args = (
util.createProp("Type", 23),
util.createProp("SubType", 127),
util.createProp("Name", "AutoNr"),
util.createProp("Content", ""),
util.createProp("Format", 4),
util.createProp("Separator", " ")
)
self.unoObjs.dispatcher.executeDispatch(
self.unoObjs.frame, ".uno:InsertField", "", 0, uno_args)