Как вставить аннотацию писателя Libreoffice с датой, используя Python - PullRequest
0 голосов
/ 18 мая 2018

Пожалуйста, обратите внимание, что для самостоятельного ответа на этот вопрос.
Большинство ссылок на com.sun.star.text.textfield.Annotation относятся к Date как :: com :: sun :: star:: util :: reference, но без конца возиться с содержимым фактически создаст аннотацию с датой.
Настройка Date.Year, Date.Month и Date.Day будет отображаться успешно, но сама аннотация по-прежнему отображается без датыт.е.

anno = model.createInstance("com.sun.star.text.textfield.Annotation")
anno.Content = "this is my annotation/comment"
anno.Author = doc.DocumentProperties.Author
anno.Date.Year = 2020
anno.Date.Month = 5
anno.Date.Day = 18

enter image description here

Ответы [ 2 ]

0 голосов
/ 19 мая 2018

Раздел 7.7.2 макро-документа Эндрю дает следующее, хотя я не проверял его:

Sub AddNoteAtCursor
    Dim vDoc, vViewCursor, oCurs, vTextField
    Dim s$
    'Lets lie and say that this was added ten days ago!'
    Dim aDate As New com.sun.star.util.Date
    With aDate
        .Day = Day(Now - 10)
        .Month = Month(Now - 10)
        .Year = Year(Now - 10)
    End With
    vDoc = ThisComponent
    vViewCursor = vDoc.getCurrentController().getViewCursor()
    oCurs=vDoc.getText().createTextCursorByRange(vViewCursor.getStart())
    s = "com.sun.star.text.TextField.Annotation"
    vTextField = vDoc.createInstance(s)
    With vTextField
        .Author = "AP"
        .Content = "It sure is fun to insert notes into my document"
        'Ommit the date and it defaults to today!'
        .Date = aDate
    End With
    vDoc.Text.insertTextContent(oCurs, vTextField, False)
End Sub

Документы API содержат ту же информацию, что и файл IDL, но их несколько легче читать.https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/textfield/Annotation.html

0 голосов
/ 18 мая 2018

Документация не всегда полная или неочевидная, в зависимости от того, где вы смотрите.
Для LibreOffice 6.0 https://api.libreoffice.org/docs/idl/ref/Annotation_8idl_source.html
Annotation.idl описывается как:

 service  com::sun::star::text::TextField;
 [property]string Author;
 [optional, property]string Initials;
 [optional, property]string Name;
 [property]string Content;
 [property]com::sun::star::util::Date Date;
 [optional, property]com::sun::star::util::DateTime DateTimeValue;

Ключевым моментом здесь является необязательный параметр DateTimeValue, который должен отображаться как элемент, который необходимо установить для предоставления аннотации даты и времени.
DateTimeValue структура с com.sun.star.util.DateTime
Для создания аннотации(с датой и временем) в документе писателя, использующем скрипт на Python, используйте в качестве шаблона следующее:

from uno import createUnoStruct
import time

def fs_Annotation(*args):
    #get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    try:
        text = model.Text
    except:
        # The macro has been called externally but LibreOffice was not running at the time
        return None
    tRange = text.End
    cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
    doc = XSCRIPTCONTEXT.getDocument()
    # you cannot insert simple text and text into a table with the same method
    # so we have to know if we are in a table or not.
    # oTable and oCurCell will be null if we are not in a table
    oTable = cursor.TextTable
    oCurCell = cursor.Cell

    anno = model.createInstance("com.sun.star.text.textfield.Annotation")

    anno.Content = "this is my annotation/comment"
    #Use documents author
    #anno.Author = doc.DocumentProperties.Author
    #Use hardcoded text
    anno.Author = "Joe Bloggs"

    t = time.localtime()
    dtv=createUnoStruct("com.sun.star.util.DateTime")
    dtv.Year = t.tm_year
    dtv.Month = t.tm_mon
    dtv.Day = t.tm_mday
    dtv.Hours = t.tm_hour
    dtv.Minutes= t.tm_min
    dtv.Seconds = t.tm_sec
    dtv.NanoSeconds = 0
    anno.DateTimeValue = dtv

    if oCurCell == None:    # Inserting into text
        text.insertTextContent(cursor, anno, True)
    else:                   # Inserting into a table
        oCurCell.insertTextContent(cursor, anno, False)
    return None

enter image description here

...