Python scriptak sortzea ScriptForge bidez

Basic eta Python arteko desberdintasunak

ScriptForge liburutegia Basic zein Python lehgoaietarako dago erabilgarri. Zerbitzu, metodo eta propietate gehienek berdin funtzionatzen dute bietan. Hala ere, lengoaia bakoitzak nola funtzionatzen duen desberdina izanda, ScriptForge erabiltzaileek liburutegiaren zenbait ezaugarri kontuan izan beharko dituzte Python erabiltzean:

Iradokizun-ikonoa

Begiratu LibreOffice Python scripten laguntza LibreOffice baitan Python scriptak nola sortu eta erabili ikasteko.


Editing Python scripts in IDEs

Python typing hints - relating to ScriptForge services public methods and properties - can be obtained from IDEs that support such facility. Visually, while editing a user script:

Ohar-ikonoa

Typing hints are displayed when editing methods and properties with propercase letters.


Python scriptak exekutatzea LibreOffice aplikazioan

Lortu nahi denaren arabera, Python scriptak LibreOffice aplikazioan erabiltzeko modu desberdinak daude:

Iradokizun-ikonoa

Scriptak LibreOffice prozesuaren barruan exekutatuko badira, gomendagarria da APSO (Alternative Script Organizer for Python) hedapena instalatzea. Python scriptak LibreOffice kanpoan garatu nahi badira, hala ere, nahi den Python IDEa aukeratu daiteke.


Scriptak LibreOffice prozesu barrutik exekutatzea

APSO hedapena erabiltzea

LibreOffice barruan Python erabiltzen hasteko biderik errazena APSO hedapen instalatzea da. Hura instalatu ondoren, ireki edozein LibreOffice osagai eta joan Tresnak - Makroak - Antolatu Python scriptak aukerara.

APSOren leiho nagusian, joan Menua - Python kontsola aukerara.

Iradokizun-ikonoa

APSO irekitzeko beste modu bat Alt + ⇧ + F11 lasterbidea erabiltzea da.


Orain, Python komandoak idazten hasi daiteke eta shell-ak komando bakoitzari dagokion irteera inprimatuko du kode-lerro bakoitza exekutatu ondoren.

ScriptForge liburutegia erabiltzen hasteko, lehenengo CreateScriptService metodoa inportatu behar da. Horren bidez, liburutegiak eskaintzen dituen zerbitzuak atzitu daitezke. Beheko adibideak Basic zerbitzua darabil mezu-koadro bat bistaratzeko.


    from scriptforge import CreateScriptService
    bas = CreateScriptService("Basic")
    bas.MsgBox("Hello!")
  

Goiko adibidea exekutatzeko, sartu lerro bakoitza Python kontsolan, banan-banan, kode-lerro bakoitza idatzi ondoren ⏎ tekla sakatuta.

Orain, Python komandoak exekutatzen hasi zaitezke ScriptForge zerbitzuak erabilita. Adibidez, beheko kode-zatiak UI zerbitzua darabil Writer dokumentu hutsa sortzeko.


    ui = CreateScriptService("UI")
    doc = ui.CreateDocument("Writer")
  

Python script-fitxategiak sortzea

Python fitxategi propioak sortu eta editatu ditzakezu testu-editore bat erabilita. Ondoren, edozein LibreOffice osagaitik deitu ahal izango dituzu.

Lehen urratsa da jakitea non gordetzen diren erabiltzaile-scriptak. Horretarako, begiratu Python scripten antolaketa eta kokalekua laguntza-orria.

Orain, testu-fitxategi bat sortu dezakezu zure Python erabiltzaile-scripten karpetan, adibidez sf_test.py, eta scripta idazten hasi.

Behean, Calc gelaxka baten zenbakizko balioa eskuratzen duen eta 1 gehitzen dion adibideko script sotil bat dago. Idatzi kode hori sf_test.py fitxategian.


    from scriptforge import CreateScriptService
    doc = CreateScriptService("Calc")
    
    def increment_cell(args=None):
        value = doc.GetValue("A1")
        value += 1
        doc.SetValue("A1", value)
    
    g_exportedScripts = (increment_cell, )
  

Adibide horrek increment_cell funtzioa sortzen du. Kontuan izan g_exportedScripts tupla bat dela eta LibreOffice aplikazioan erabiltzaile-script modura bistaratuko diren funtzioak adierazten dituela.

Script hau Calc dokumentu baten barrutik exekutatzeko:

  1. Sortu edo ireki Calc fitxategi bat.

  2. Sartu zenbaki bat uneko orriko "A1" gelaxkan.

  3. Joan Tresnak - Makroak - Exekutatu makroak aukerara.

  4. Aukeratu 'Nire makroak - sf_test' liburutegi-hautatzailean. Ondoren, aukeratu Makroaren izena zerrendan ageri den increment_cell funtzioa.

  5. Sakatu Exekutatu. Begiratu nola "A1" gelaxkako balioari 1 gehitu zaion.

APSO ere erabili daiteke Python scriptak antzera exekutatzeko:

  1. Lehenengo, ireki APSO Tresnak - Makroak - Antolatu Python scriptak erabilita.

  2. Makroen zerrendan, joan Nire makroak - sf_test - increment_cell aukerara.

  3. Sakatu Exekutatu.

Scriptak LibreOffice prozesutik bereizita exekutatzea

Instalazioaren bide-izena zehaztea

Scriptak prozesu bereizi batetik exekutatzeko lehen urratsa LibreOffice zein karpetatan dagoen instalatuta aurkitzea da. Hori egiteko modu bat baino gehiago dago, baina ScriptForge liburutegiak zure instalazioaren bide-izena identifikatzeko modu azkarra eskaintzen du. Ireki APSOren Python kontsola eta idatzi:


    from scriptforge import CreateScriptService
    fs = CreateScriptService("FileSystem")
    fs.FileNaming = "SYS"
    inst_dir = fs.InstallFolder
    print(inst_dir)
  

Goiko kodearen irteera LibreOffice instalatuta dagoen oinarri-direktorioa da. Orain, "program" azpikarpeta emaitza gisa lortutako bide-izen horri gehitu behar zaio. Hori da Python scriptak prozesu bereizi batetik exekutatzeko erabili behar den oinarri-karpeta.

Adibidez, demagun /usr/lib/libreoffice/ eskuratu dela goiko Python kodea exekutatu ondoren. Kasu horretan, /usr/lib/libreoffice/program erabili beharko da Python scriptak exekutatzeko bide-izen gisa.

Start LibreOffice with pipe or socket settings

Python scriptak prozesu bereizi batetik exekutatzeko, LibreOffice zenbait aukera gehigarrirekin abiarazi behar da, kanpoko prozesuak LibreOffice osagai-prozesuarekin komunikatzeko erabiliko dituen ostalari-izena eta ataka zehazteko.

Open the your operating system's command prompt, navigate to the program folder of your LibreOffice installation directory and type either:

./soffice --accept='pipe,name=libreoffice;urp;'

./soffice --accept='socket,host=localhost,port=2021;urp;'

Either command above will start LibreOffice with a communication channel open so that other processes can exchange messages with it.

Note that the previous example opens LibreOffice start center. If you want to open a specific component, for instance Writer, you can add the --writer flag to the command, as follows.

./soffice --writer --accept='socket,host=localhost,port=2021;urp;'

Take note of the name, or host and port parameters, which in this example are libreoffice, or localhost and 2021, respectively.

Kanpoko Python kontsola bat exekutatzea

Abiarazi Python kontsola zure LibreOffice instalazioaren bide-izenean dagoen program karpetatik. Jarraitu goiko urratsak zure instalazioaren bide-izena aurkitzeko.

Linux / Mac OS sistemetan:

$ cd /usr/lib/libreoffice/program

$ python

Windows sisteman:

$ cd C:\Program Files\LibreOffice\program\

$ python.exe

This will open the Python shell and now you can start typing commands that will be executed by LibreOffice. But first you need to set up the pipe or the socket connection. The ScriptForge() statement below must precede the very first call to CreateScriptService().

Run either:


   from scriptforge import ScriptForge, CreateScriptService
   ScriptForge(pipe='libreoffice')

    from scriptforge import ScriptForge, CreateScriptService
    ScriptForge(hostname='localhost', port=2021)
  
Ohar-ikonoa

Read the section Setting PYTHONPATH below in case of errors importing scriptforge.py or uno.py.


The second line of code above defines the pipe or host and port settings so that the Python shell can communicate with an ongoing LibreOffice process opened with the same pipe or socket settings.

Orain beste Python komando batzuk exekutatu ahal dituzu, eta horiek LibreOffice prozesuarekin komunikatu ahal izango dira. Adibidez:


    ui = CreateScriptService("UI")
    bas = CreateScriptService("Basic")
    doc = ui.OpenDocument("~/Documents/myFile.ods")
    bas.MsgBox(doc.DocumentType)
  

Setting PYTHONPATH

Depending on your operating system's configuration you will need to set the environment variable PYTHONPATH in order to import the scriptforge.py library, which in turn requires importing the uno.py library.

Use your operating system's file search tool to determine the directory where both these files are located.

For instance, on a default Ubuntu installation both files may be located at:

In this case, set the environment variable PYTHONPATH as follows before starting the Python interpreter:

export PYTHONPATH=/usr/lib/libreoffice/program:/usr/lib/python3/dist-packages

Ohar-ikonoa

The location of these files will be different for each operating system and LibreOffice installation method.


Emaguzu laguntza!

Emaguzu laguntza!