LibreOffice作家允许用户在文本中插入注释(注释/注释)。如何从python宏读取LibreOffice作家注释的内容
我的问题是我无法找到一种方法来访问特定行注释的内容。
以下python代码会查找选定/突出显示的文本,然后去除除格式化时间码(例如01:10:23或11:10)之外的所有内容,并将其转换为秒。
如果未选择文本,则会选择整个当前行并尝试查找时间代码。但是,时间码可能在注释中。
我已经成功地获得了文档中所有注释的列表,在代码的开头注释掉了,但它对我来说没有用处。
我一直无法发现占卜
的方法)当前行是否有一个注释或
二)如何访问其内容。
如果有人设法做到这一点,我会很感激任何指针。
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=(':')
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
枚举注释和用[getAnchor()](https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/XTextContent.html#getAnchor)至找出每个所在的位置。请参阅https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Editing_Text#Text_Contents_Other_Than_Strings。 –
感谢您的回复。也许是我,但我无法找到从oField或oField.getAnchor()返回的任何方法,当我枚举oEnum,提供文档中的注释位置。如果我创建一个游标并尝试“gotoRange(oField,False)”,它声称没有这样的接口。我在这个问题上加入了我的错误尝试,希望你能发现明显的错误。我承认会在API文档上苦苦挣扎,就像游泳在糖果上一样 –