如何在 libreoffice writer 中编写 python 宏来发送接收数据

Posted

技术标签:

【中文标题】如何在 libreoffice writer 中编写 python 宏来发送接收数据【英文标题】:How do I write a python macro in libreoffice writer to send a receive data 【发布时间】:2015-03-06 10:01:44 【问题描述】:

我在 libreoffice writer 中工作,需要向另一个我知道正在侦听 TCP 端口的程序发送请求,该程序将返回需要插入到当前 Writer 文档中的数据。 该宏需要允许将数据作为普通打开的文本插入或插入到当前光标所在的表格中的单元格中。 UNO 文档非常不清楚且难以浏览。

【问题讨论】:

我没想到西班牙宗教裁判所。希望我现在已经满意地遵守了。 几乎没有宗教裁判所!没有答案的问题是“未回答的”,很多人花时间寻找解决问题,不这样做会减慢他们的速度。鼓励自我回答,看到你没有得到一个赞成票,而是两个;)我知道找到好的 uno 文档有多么困难,所以请坚持下去。哦,几天后不要忘记将解决方案奖励给自己。 我宁愿希望,没有人会期待西班牙宗教裁判所!也许我暴露了我的年龄!谢谢你的鼓励。 【参考方案1】:
def fs2ClientdataDoc(*args):
#get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()                                      
    model = desktop.getCurrentComponent()
#get the XText interface
    try:
        text = model.Text
    except AttributeError:
        raise Exception("This script is for Writer Documents only")

#create an XTextRange at the end of the document
    tRange = text.End
    cursor = Desktop.getCurrentComponent().getCurrentController().getViewCursor()
# your 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
#and get the string from Footswitch2 via a TCP port
    import os, socket, time
    from configobj import ConfigObj
    configuration_dir = os.environ["HOME"]
    config_filename = configuration_dir + "/fs2.cfg"
    if os.access(config_filename, os.R_OK):
        pass
    else:
        return None
    cfg = ConfigObj(config_filename)
#define values to use from the configuration file
    tcp_port = int(cfg["control"]["TCP_PORT"])
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(0.5)
    try:
        sock.connect(("localhost", tcp_port))
    except:
        return None
    sock.settimeout(5)
    try:
        sock.send(bytes('client\n', 'UTF-8'))
    except:
        return None
    try:
        time.sleep(1.0)
        s_list = sock.recv(4096).decode('UTF-8')
        s_list = s_list.split("\n")
    except:
        return None
    lines_in_response = len(s_list)
    if lines_in_response is None:
        return None
    for x in range(0,lines_in_response):
        insert_text = s_list[x]+"\n"
        if oCurCell == None: # Are we inserting into a table or not?
            text.insertString(cursor, insert_text, 0)
        else:
            insert_table = s_list[x].split("\n")
            parts = len(insert_table)
            for y in range(0,parts):
                it = insert_table[y]
                cell = oTable.getCellByName(oCurCell.CellName)
                cellCursor = cell.createTextCursor() # use this cursor if you want the text inserted at the beginning of the cell
                cell.insertString(cellCursor, it, False)
        #        cell.insertString(cursor, insert_text, False) # use the standard cursor to insert at the current position
                cursor.goDown(1,False)
                cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
                oTable = cursor.TextTable
                oCurCell = cursor.Cell
    sock.close()
    return None

【讨论】:

以上是关于如何在 libreoffice writer 中编写 python 宏来发送接收数据的主要内容,如果未能解决你的问题,请参考以下文章

如何将 python 源代码添加到 libreoffice writer?

如何在 libreoffice writer 中编写 python 宏来发送接收数据

如何在 Libreoffice writer 的标题下添加边框

如何在 libreoffice writer 中使用 python 宏将文本插入斜体或粗体等

我们如何在 Visual Basic 6 中导入 LibreOffice Writer 的监听器事件

如何将(静态)内容放在 Libreoffice Writer 每一页的右栏中?