Jump to content

Configure PyCharm in Windows

From The Document Foundation Wiki


How to configure PyCharm in Windows to show ScriptForge hints in Python scripts

Authored by Jean-Pierre Ledure.

Advanced Python IDE's like PyCharm or VSCode utilize type hints to offer autocomplete suggestions during code writing by analyzing type annotations and combining them with the code context. Type hints, also referred to as type annotations, are comments embedded in the code specifying the data types of variables, parameters, and return values. Python scripts may benefit from the support of type hints when using the ScriptForge API.

Look at the illustration below, where the SetArray() method of the Calc service is detailed in a popup when the mouse is hovering a SetArray invocation.

The procedure described here uses a pipe as communication channel between the Python script and LibreOffice, during the test phase. Afterwards, it can be stored in a document or in a central location with a minimal change.

Preconditions:

  • PyCharm 2024.3.4 (Community Edition)
  • LibreOffice Version: 25.2.1.2 (X86_64) / LibreOffice Community
  • OS: Windows 10 X86_64

1. START LIBREOFFICE Run LibreOffice with

   "C:\Program Files\LibreOffice\program\soffice.exe" --accept=pipe,name=libreoffice;urp;

Note that the additional --accept argument does not harm the usual interactive mode. It only adds an alternative channel. The name of the pipe may obviously be modified.

2. START PYCHARM (below it is the scenario at a first fresh start of the software)

a) Set the Python interpreter shipped with LibreOffice as the preferred one

[Left pane]

   Customize
   
   Settings icon
      Settings
          Python interpreter
              Add interpreter
                 Add Local Interpreter
                     Environment: Select existing
                        Type: Python
                        Python path: C:\Program Files\LibreOffice\program\python.exe
                        Apply + OK
          Python Interpreter
             Select the added interpreter Python 3.10 C:\Program Files\LibreOffice\program\python.exe

b) Create your first script in a new project called (for instance) LibreOffice

   Projects
   
    New Project
       Pure Python
         Location: C:\Users\jp\PycharmProjects\LibreOffice
         Interpreter type: Custom environment
         Environment: Select existing
         Type: Python
         Python path: (the interpreter is now correct by default)
   Project
       LibreOffice [right-click]
         New
           Python file
           Name: Demo.py

c) Insert a code snippet in the Demo.py file. Suggestion, at the end of the page.

Walk your mouse above MsgBox on line 4, run the script, ...


Python hints in PyCharm with LibreOffice and ScriptForge scripts
Python hints in PyCharm with LibreOffice and ScriptForge scripts


from scriptforge import CreateScriptService, ScriptForge

ScriptForge(pipe = 'libreoffice')   # Activate the pipe, remove this statement in normal mode
basic = CreateScriptService('Basic')
basic.MsgBox('OK ?')