如何使用Python插入带有日期的Libreoffice编写器注释

时间:2018-05-18 15:43:22

标签: python libreoffice uno libreoffice-writer

请注意,这是一个自行回答的问题供参考。
大多数对 // get the layout from the source slide XSLFSlideLayout layout = srcSlide.getSlideLayout(); XSLFSlide newslide = ppt .createSlide(defaultMaster.getLayout(layout.getType())) .importContent(srcSlide); XSLFNotes srcNotes = srcSlide.getNotes(); XSLFNotes newNotes = ppt.getNotesSlide(newslide); newNotes.importContent(srcNotes); 的引用都将com.sun.star.text.textfield.Annotation称为:: com :: sun :: star :: util :: reference,但是对于内容的摆弄无法实际创建注释约会。
设置Date.Year,Date.Month和Date.Day会显示成功,但注释本身仍然没有日期,即

Date

enter image description here

2 个答案:

答案 0 :(得分:0)

根据您所查看的位置,文档并不总是完整或不明显 对于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

答案 1 :(得分:0)

安德鲁的宏观文件第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

相关问题