[英]How to insert a Libreoffice writer Annotation with a date using Python
Please note, this is a self answered question for reference. 请注意,这是一个自我回答的问题,仅供参考。
Most of the references to com.sun.star.text.textfield.Annotation
refer to Date
as being a :: com :: sun :: star :: util :: reference but no end of fiddling about with the contents will actually create an annotation with a date. 大部分对
com.sun.star.text.textfield.Annotation
引用都将Date
称为:: :: com :: sun :: star :: util ::引用,但对内容的摆弄无休止实际上会创建一个带日期的注释。
Setting Date.Year, Date.Month and Date.Day will appear successful but the annotation itself still appears without a date ie 设置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
The documentation is not always complete, or is not obvious, depending upon where you look. 文档并不总是完整的,也不是显而易见的,具体取决于您所处的位置。
For LibreOffice 6.0 https://api.libreoffice.org/docs/idl/ref/Annotation_8idl_source.html 对于LibreOffice 6.0 https://api.libreoffice.org/docs/idl/ref/Annotation_8idl_source.html
The Annotation.idl is described as: 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;
The key here is the optional DateTimeValue
which it would appear is the item that needs to be set to provide the Annotation with a Date and Time. 此处的关键是可选的
DateTimeValue
,它将显示为需要为Annotation提供日期和时间的项目。
DateTimeValue
structure is from com.sun.star.util.DateTime
DateTimeValue
结构来自com.sun.star.util.DateTime
To create an annotation (with a date and time) in a writer document using a python script, use the following as a template. 要使用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
Section 7.7.2 of Andrew's macro document gives the following, although I have not tested it. 尽管我尚未对其进行测试,但安德鲁的宏文档的第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
The API docs contain the same information as the IDL file but are somewhat easier to read. API文档包含与IDL文件相同的信息,但更易于阅读。 https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/textfield/Annotation.html
https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/textfield/Annotation.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.