Pyuno索引问题,我想解释一下

时间:2015-09-05 09:52:44

标签: python libreoffice uno

以下python libreoffice Uno宏可以工作,但只能使用try..except语句 宏允许您选择编写器文档中的文本,并将其发送到默认浏览器中的搜索引擎 问题是,如果您选择单个文本,则会填充oSelected.getByIndex(0),但如果您选择了多个文本oSelected.getByIndex(0),则不会填充。在这种情况下,数据从oSelected.getByIndex(1)开始,oSelected.getByIndex(0)留空 我不知道为什么会这样,并且想知道是否有人可以解释这种奇怪的行为。

#!/usr/bin/python    
import os
import webbrowser
from configobj import ConfigObj
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY
from com.sun.star.awt.MessageBoxButtons import DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE

from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX

def fs3Browser(*args):
#get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    doc = XSCRIPTCONTEXT.getDocument()
    parentwindow = doc.CurrentController.Frame.ContainerWindow

    oSelected = model.getCurrentSelection()
    oText = ""
    try:
        for i in range(0,4,1):
            print ("Index No ", str(i))
            try:
                oSel = oSelected.getByIndex(i)
                print (str(i), oSel.getString())
                oText += oSel.getString()+" "
            except:
                break
    except AttributeError:
        mess = "Do not select text from more than one table cell"
        heading = "Processing error"
        MessageBox(parentwindow, mess, heading, INFOBOX, BUTTONS_OK)
        return

    lookup = str(oText)
    special_c =str.maketrans("","",'!|@#"$~%&/()=?+*][}{-;:,.<>')
    lookup = lookup.translate(special_c)
    lookup = lookup.strip()
    configuration_dir = os.environ["HOME"]+"/fs3"
    config_filename = configuration_dir + "/fs3.cfg"
    if  os.access(config_filename, os.R_OK):
        cfg = ConfigObj(config_filename)

    #define search engine from the configuration file
    try:
        searchengine = cfg["control"]["ENGINE"]
    except:
        searchengine = "https://duckduckgo.com"
    if 'duck' in searchengine:
        webbrowser.open_new('https://www.duckduckgo.com//?q='+lookup+'&kj=%23FFD700 &k7=%23C9C4FF &ia=meanings')
    else:
        webbrowser.open_new('https://www.google.com/search?/&q='+lookup)
    return None
def MessageBox(ParentWindow, MsgText, MsgTitle, MsgType, MsgButtons):
    ctx = XSCRIPTCONTEXT.getComponentContext()
    sm = ctx.ServiceManager
    si = sm.createInstanceWithContext("com.sun.star.awt.Toolkit", ctx) 
    mBox = si.createMessageBox(ParentWindow, MsgType, MsgButtons, MsgTitle, MsgText)
    mBox.execute()    

1 个答案:

答案 0 :(得分:1)

您的代码缺少某些内容。这可以在不需要额外的try/except子句的情况下工作:

selected_strings = []
try:
    for i in range(oSelected.getCount()):
        oSel = oSelected.getByIndex(i)
        if oSel.getString():
            selected_strings.append(oSel.getString())
except AttributeError:
    # handle exception...
    return
result = " ".join(selected_strings)

回答关于&#34;奇怪行为的问题,&#34;这对我来说似乎很简单。如果第0个元素为空,则有多个选择可能需要以不同方式处理。