Make a autocompletion with python

Hi!

I would like to create an autocomplete program with python for libreoffice writer.
So, I have a file with all the sentences I usually write on libreoffice writer. I would like to create an extension that offers me directly the sentence when I write the beginning.

It is the same as tabnine (https://www.tabnine.com/).

On this picture, I write “prin” and the tabnine proposes 2 sentences.
And I can select one of them with the ‘tab’ key:

I can make the extension with any language (basic, python, c++ and …)
Thank you in advance for your help

You are aware that there are already stand-alone Python IDEs with code completion and the like?

What is the question? It is up to you to write the code, but if you get stuck, post a question here and we can help.

If you haven’t yet, see how Writer Word Completion works. That’s in Tools > Auto Correct > Auto Correct Options > Word Completion. The key to choose the next word from the auto complete list is Ctrl + Tab. That’s probably similar to how your extension should work.

If you are looking for some general advice, I would recommend Python for writing LibreOffice extensions. Java is good as well but has some drawbacks including being tied to a specific architecture and requiring queryInterface statements. Also, echoing what @anon87010807 said, if your goal is an IDE, then Writer is probably not the best choice. An IDE such as PyCharm or a plain text editor would be better.

Thank you @anon87010807 and @jimk for your answer

I don’t want to turn Libreoffice into an IDE for python (i use vim).
I would like to optimize the process of creating my father’s quotes. He usually writes the same sentence:

Interior layout large closet.
Small closet interior layout.
Adjustment of the opening strike of the panic bar.
Adjustment of the whole.
Adjustment of the sashes on the frames.
Adjustment of the sashes on the frames.
Adjustment of the shades, setting in wax.
Adjustment of the colors, waxing.
Adjustment of the frame in the dormer window
Adjustment of the frame in the dormer window

This is a 10 lines example but I have a file of about 7000 lines.
The image, which I posted last time, was an example of what I would like to do with LibreOffice.

LibreOffice’s word completion is a bit “poor”. Because I would like to automate sentences, not only words, and I would like to set up an AI that can change some elements of the sentence, like the type of material.

If I understand, a way do it is to use queryInterface to draw
the sentence at the bottom of the text cursor.

For information, i use LibreOffice 6.4.7.2 on Ubuntu 20.04.

Thank you in advance for your answer !

Me too. :slight_smile:

That doesn’t sound right. queryInterface is used in Java macros to get an interface of an UNO object. It is not a specific call that would, for example, draw something. Also it is not needed in Python or Basic because those languages can use any UNO interface of an object and they do not need to specify the variable type.

It sounds like you already have some idea or reference information in mind. Please post links to what you have researched so far. Or, as I mentioned earlier, write some code and explain what you have tried so far.

Maybe your topic is too broad. Instead, ask something like, “How do I temporarily display text at the bottom of the text cursor, such as a tooltip?” You could post a new question with a link to this one for context.

In contrast to @anon87010807’s new comment below, I can imagine this working conveniently in Writer or Calc depending on the design and UI choices you make. For example, one possibility would be to pop up a dialog with the matches. LibreOffice macros can create dialogs dynamically or with the built-in dialog editor. I would rather see it done with LO UI tools instead of Python tkinter or similar, for a more integrated user experience.

That sounds interesting, and is much different from what I thought you were after. If it’s for your father’s business, I wonder if there isn’t already dedicated software for selecting job descriptions from a list stored in a database that does exactly what you want. Anyway, I’d make the entire UI in Python, so you generate the quotes list in your app, and when that’s ready, copy & paste the end result in a Calc sheet, or do you really want it in Writer? Allowing the user to work in either Writer or Calc and have your Python code interfere with what LibO is already doing seems to me impractical.

Thank you @anon87010807 and @jimk for your answers.

As @jimk says, I’m going to create a new topic especially for the question of “How to temporarily display text at the bottom of the text slider, like a tooltip?”. Because this is my first problem because I need to solve it to do the following parts ( get the user input on LO and propose the right sentences ).

@anon87010807 your idea is very interesting but it is not directly what I am looking for. LO, I will just disable word completion. As @jimk says, LO UI tools instead of Python tkinter or similar, for a more integrated user experience. and I will easier for my father. Creating a whole application is a lot of trouble, UI, error for the user and … In addition, it is not very modular.

With LO, I just create the environment to execute python code.
The code that I run is:

import uno

def my_first_macro_writer():
    doc = XSCRIPTCONTEXT.getDocument()
    text = doc.getText()  # com.sun.star.text.Text
    text.setString('Hello World in Python in Writer\n mias ui')
    return

I did a lot of searching with the words ‘libreoffice’, ‘python’, ‘uno’ or/and ‘autocompletion’ but I didn’t find anything.

I am currently studying computer science and have done some projects. (If you are very interesting : https://github.com/titicplusplus )

Thank you in advance.