0

OpenOfficeドキュメントで「変数」を定義しようとしていますが、フィールドを使用して変数の値を表示しようとすると、空の文字列しか表示されないため、何か間違ったことをしている必要があります。

これが私が使用しているコードです(Python UNOブリッジを使用)。興味深いのは2番目の関数です。

import time
import subprocess
import logging
import os
import sys
import uno
from com.sun.star.text.SetVariableType import STRING

def get_uno_model(): # helper function to connect to OOo. Only interesting 
                     # if you want to reproduce the issue locally, 
                     # don't spend time on this one
    try:
        model = XSCRIPTCONTEXT.getDocument()
    except NameError:
        pass # we are not running in a macro
    # 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
    try:
        ctx = resolver.resolve("uno:socket,host=localhost,port=2002;"
                               "urp;StarOffice.ComponentContext")
    except:
        cmd = ['soffice', '--writer', '-accept=socket,host=localhost,port=2002;urp;']
        popen = subprocess.Popen(cmd)
        time.sleep(1)
        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)

    # access the current writer document
    model = desktop.getCurrentComponent()
    return model

def build_variable(model, name, value):
    # find or create a TextFieldMaster with the correct name
    master_prefix = u"com.sun.star.text.fieldmaster.SetExpression"
    variable_names = set([_name.split('.')[-1] 
                          for _name in model.TextFieldMasters.ElementNames
                          if _name.startswith(master_prefix)])
    master_name = u'%s.%s' % (master_prefix, name)
    if name not in variable_names:
        master = model.createInstance(master_prefix)
        master.Name = name
    else:
        master = model.TextFieldMasters.getByName(master_name)

    # create the SetExpression field
    field = model.createInstance(u'com.sun.star.text.textfield.SetExpression')
    field.attachTextFieldMaster(master)
    field.IsVisible = True
    field.IsInput = False
    field.SubType = STRING
    field.Content = value
    return field

model = get_uno_model() # local function to connect to OpenOffice
text = model.Text
field = build_variable(model, u'Toto', 'nice variable')
text.insertTextContent(text.getEnd(), field, False)

このコードは(削除しすぎない限り)何らかの形で機能しますが、Totoの値を表示するためにフィールドを手動で挿入すると、期待する「nice variable」文字列が得られず、挿入されたフィールドに値がありません。

4

2 に答える 2

0

マスターフィールドの作成後にマスターフィールドのSubTypeプロパティをSTRINGに設定するコードがありません。

于 2013-01-16T18:54:40.267 に答える
0

試す

field.CurrentPresentation = value

これで問題が解決するかどうかお知らせください。ありがとうございます。

于 2013-03-19T09:18:26.873 に答える