Rolf of Saxony
Rolf of Saxony

Reputation: 22443

How do I insert text as Italic or Bold etc with a python macro in libreoffice writer

The premise:
I am working in libreoffice writer and need to send an instruction to another program that I know is listening on a TCP port, via a python macro.

I am expecting a reply from the listening program and want to insert the replied data into the libreoffice document in italics.

Upvotes: 1

Views: 1800

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

This code allows the data to be inserted as normal open text or into a cell within a table in which the cursor is presently positioned or the beginning of the cell, in Italics and then resets to normal text for subsequent input. For Bold text refer to com.sun.star.awt.FontWeight

def fs2_Unclear_upper(*args):
    import socket, time
    class FontSlant():
        from com.sun.star.awt.FontSlant import (NONE, ITALIC,)
#get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    text = model.Text
    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
    import os
    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(0.5)
    try:
        sock.send(bytes('get_time\n', 'UTF-8'))
    except:
        return None
    try:
        time.sleep(0.2)
        s_list = sock.recv(1024).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
    time_stamp = s_list[0]
    insert_text = "[Unclear " + time_stamp + "]"
    Text_Italic = FontSlant.ITALIC
    Text_None = FontSlant.NONE
    cursor.CharPosture=Text_Italic
    if oCurCell == None: # Are we inserting into a table or not?
        text.insertString(cursor, insert_text, 0)
    else:
        cell = oTable.getCellByName(oCurCell.CellName)
        cell.insertString(cursor, insert_text, False)
    cursor.CharPosture=Text_None
    sock.close()
    return None 

for plain BOLD change the code above to use the following:

class FontWeight():
    from com.sun.star.awt.FontWeight import (NORMAL, BOLD,)

Text_Bold = FontWeight.BOLD
Text_Normal = FontWeight.NORMAL
cursor.CharWeight=Text_Bold

cursor.CharWeight=Text_Normal

to replace the code dealing with slant and posture above

Upvotes: 2

Related Questions