代码之家  ›  专栏  ›  技术社区  ›  Rolf of Saxony

如何使用python插入日期为的libreoffice writer注释

  •  0
  • Rolf of Saxony  · 技术社区  · 6 年前

    请注意,这是一个自我回答的问题,供参考。
    大多数参考文献 com.sun.star.text.textfield.Annotation 请参阅 Date 作为一个::com::sun::star::util::reference,但是对内容的任何修改都不会真正创建一个带有日期的注释。
    设置日期.年,日期.月和日期.日将显示为成功,但注释本身仍然显示为没有日期,即

    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 回复  |  直到 6 年前
        1
  •  0
  •   Rolf of Saxony    6 年前

    文档并不总是完整的,或者不明显,这取决于您查看的位置。
    对于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 它将显示的是需要设置为向注释提供日期和时间的项。
    日期时间值 结构来自 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

        2
  •  0
  •   Jim K    6 年前

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

    推荐文章