1

LibreOffice ライターを使用すると、ユーザーはテキスト内に注釈 (メモ/コメント) を挿入できます。

私の問題は、行固有の注釈の内容にアクセスする方法が見つからないことです。
次の Python コードは、選択/強調表示されたテキストを検索し、フォーマットされたタイム コード (01:10:23 または 11:10 など) を除くすべてを取り除き、秒に変換します。
テキストが選択されていない場合は、現在の行全体を選択し、タイム コードを見つけようとします。ただし、タイム コードは注釈に含まれる場合があります。

ドキュメント内のすべての注釈のリストを取得し、コードの先頭でコメントアウトしましたが、役に立ちません。

私は占いの方法を発見することができませんでした

a) 現在の行に注釈があるかどうか、または
b) その内容にアクセスする方法。

誰かがこれを達成できた場合は、ポインタをいただければ幸いです。

def fs2_GoToTimestamp(*args):
#get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    oSelected = model.getCurrentSelection()
#access annotations for the whole document
#    oEnum = model.getTextFields().createEnumeration()
#    cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
#    while oEnum.hasMoreElements():
#        oField = oEnum.nextElement()
#        cursor.gotoRange(oField,False)
#        print (cursor.getPosition())
#        if oField.supportsService('com.sun.star.text.TextField.Annotation'):
#            print (oField.Content)
#            x = oField.getAnchor()
#            print (dir(x))
    oText = ""
    try: #Grab the text selected/highlighted
        oSel = oSelected.getByIndex(0)
        oText= oSel.getString()
    except:pass
    try:
        if oText == "": # Nothing selected grab the whole line
            cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
            cursor.gotoStartOfLine(False) #move cursor to start without selecting (False)
            cursor.gotoEndOfLine(True) #now move cursor to end of line selecting all (True)
            oSelected = model.getCurrentSelection()
            oSel = oSelected.getByIndex(0)
            oText= oSel.getString()
            # Deselect line to avoid inadvertently deleting it on next keystroke
            cursor.gotoStartOfLine(False)
    except:pass
    time = str(oText)
    valid_chars=('0123456789:')
    time = ''.join(char for char in time if char in valid_chars)
    if time.count(":") == 1:
        oM, oS = time.split(":")
        oH = "00"
    elif time.count(":") == 2:
        oH,oM,oS = time.split(":")
    else:
        return None
    if len(oS) != 2:
        oS=oS[:2]
    try:
        secs = int(oS)
        secs = secs + int(oM) * 60
        secs = secs + int(oH) *3600
    except:
        return None
    seek_instruction = 'seek'+str(secs)+'\n'
    #Now do something with the seek instruction 
4

2 に答える 2

0

Jim Kの助けを借りて、自己回答を以下に掲載します。最も役立つと思われる場所についてコメントしました。

#!/usr/bin/python
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK
from com.sun.star.awt.MessageBoxType import INFOBOX
def fs2_GoToTimestamp(*args):
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    oSelected = model.getCurrentSelection()
    doc = XSCRIPTCONTEXT.getDocument()
    parentwindow = doc.CurrentController.Frame.ContainerWindow
    cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
    try:
        CursorPos = cursor.getText().createTextCursorByRange(cursor)#Store original cursor position
    except:# The cursor has been placed in the annotation not the text
        mess = "Position cursor in the text\nNot the comment box"
        heading = "Positioning Error"
        MessageBox(parentwindow, mess, heading, INFOBOX, BUTTONS_OK)
        return None
    oText = ""
    try: #Grab the text selected/highlighted
        oSel = oSelected.getByIndex(0)
        oText= oSel.getString()
    except:pass
    try:
        if oText == "": # Nothing selected grab the whole line
            store_position = 0
            cursor.gotoStartOfLine(False) #move cursor to start without selecting (False)
            cursor.gotoEndOfLine(True) #now move cursor to end of line selecting all (True)
            oSelected = model.getCurrentSelection()
            oSel = oSelected.getByIndex(0)
            oText= oSel.getString()
            y = cursor.getPosition()
            store_position = y.value.Y
            # Deselect line to avoid inadvertently deleting it on next user keystroke
            cursor.gotoStartOfLine(False)

            if oText.count(":") == 0:
            # Still nothing found check for an annotation at this location
            #enumerate through annotations for the whole document
                oEnum = model.getTextFields().createEnumeration()
                while oEnum.hasMoreElements():
                    oField = oEnum.nextElement()
                    if oField.supportsService('com.sun.star.text.TextField.Annotation'):
                        anno_at = oField.getAnchor()
                        cursor.gotoRange(anno_at,False)
                        pos = cursor.getPosition()
                        if pos.value.Y == store_position: # Found an annotation at this location
                            oText = oField.Content
                            break
                # Re-set cursor to original position after enumeration & deselect
                cursor.gotoRange(CursorPos,False)
    except:pass

    time = str(oText)
    valid_chars=('0123456789:')
    time = ''.join(char for char in time if char in valid_chars) #Strip out all invalid characters
    if time.count(":") == 1: # time 00:00
        oM, oS = time.split(":")
        oH = "00"
    elif time.count(":") == 2: # time 00:00:00
        oH,oM,oS = time.split(":")
    else:
        return None
    if len(oS) != 2: # in case time includes tenths 00:00.0 reduce to whole seconds
        oS=oS[:2]
    try:
        secs = int(oS)
        secs = secs + int(oM) * 60
        secs = secs + int(oH) *3600
    except:
        return None
    seek_instruction = 'seek'+str(secs)+'\n'
    print("Seconds",str(secs))
    # Do something with seek_instruction

def MessageBox(ParentWindow, MsgText, MsgTitle, MsgType, MsgButtons):
    ctx = XSCRIPTCONTEXT.getComponentContext()
    sm = ctx.ServiceManager
    si = sm.createInstanceWithContext("com.sun.star.awt.Toolkit", ctx)
    mBox = si.createMessageBox(ParentWindow, MsgType, MsgButtons, MsgTitle, MsgText)
    mBox.execute() 
于 2017-06-24T12:27:09.983 に答える