gpt4 book ai didi

python - 如何从 python 宏读取 LibreOffice writer 注释的内容

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:37 25 4
gpt4 key购买 nike

LibreOffice writer 允许用户在文本中插入注释(注释/评论)。

我的问题是我无法找到访问特定行注释内容的方法。
以下 python 代码查找选定/突出显示的文本,然后删除除格式化时间代码(例如 01:10:23 或 11:10)之外的所有内容,并将其转换为秒。
如果未选择任何文本,它将选择整个当前行并尝试查找时间代码。但是,时间代码可以位于注释中。

我已经设法获得文档中所有注释的列表,并在代码开头注释掉,但这对我来说没有用。

我一直无法找到占卜的方法

a) 当前行是否有注释或
b) 如何访问其内容。

如果有人成功实现了这一点,我将不胜感激。

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=('0123456789:')
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

最佳答案

枚举annotations并使用getAnchor()找出每个位置的位置。此答案基于https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Editing_Text#Text_Contents_Other_Than_Strings .

您的代码即将运行。

while oEnum.hasMoreElements():
oField = oEnum.nextElement()
if oField.supportsService('com.sun.star.text.TextField.Annotation'):
xTextRange = oField.getAnchor()
cursor.gotoRange(xTextRange, False)

XrayTool 或 MRI 等内省(introspection)工具可以提供更好的信息,而不是 print (dir(x))。它使 API 文档更容易理解。

关于python - 如何从 python 宏读取 LibreOffice writer 注释的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44703487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com