Макросы/Руководство Python/Введение

    From The Document Foundation Wiki
    This page is a translated version of the page Macros/Python Guide/Introduction and the translation is 22% complete.
    Outdated translations are marked like this.


    Other languages:

    Return to Index


    Введение

    Что такое PyUNO?

    LibreOffice позволяет пользователям писать макросы на различных интерпретируемых языках, один из которых Python. PyUNO - это компонент, который дает пользователям доступ к API LibreOffice из Python.

    Установка

    На некоторых операционных системах, таких, как Ubuntu 18.04 LTS вам нужно установить дополнительный пакет OS. В Ubuntu такой пакет называется libreoffice-script-provider-python и содержит такие файлы, как scriptproviderforpython.rdb (метаданные XML) и pythonscript.py (инфраструктура Python).

    • In Ubuntu 18.04+

    sudo apt install libreoffice-script-provider-python

    Поддержка тестов для макросов Python

    Откройте новый документ в Writer. Выберите пункт меню Сервис ▸ Макросыs ▸ Выполнить макрос..., откроется диалог "Выбор макроса". В списке "Библиотека" выберите Макросы LibreOffice ▸ HelloWorld, в списке "Имя макроса" выберите HelloWorldPython и нажмите кнопку Выполнить

    Если вы видите этот результат, ваша система может выполнять макросы Python.

    Test macro python

    Где хранятся макросы?

    Каталог с профилем пользователя, макросы доступны только для пользователя

    GNU/Linux
    /home/USER/.config/libreoffice/4/user/Scripts/python
    Windows
    %APPDATA%\LibreOffice\4\user\Scripts\python
    OSX
    ~/Library/Application Support/LibreOffice/4/user/Scripts/python/

    There is no built-in way to edit Python scripts so you have to use your own text editor. There are 3 places where you can put your code.

    Profile USER folder, macros available only for USER

    GNU/Linux
    /home/USER/.config/libreoffice/4/user/Scripts/python
    Windows
    %APPDATA%\LibreOffice\4\user\Scripts\python
    macOS
    ~/Library/Application Support/LibreOffice/4/user/Scripts/python/

    Каталог LibreOffice, макросы доступны для всех пользователей

      • GNU/Linux
        • /usr/lib/libreoffice/share/Scripts/python/
    • GNU/Linux
    /usr/lib/libreoffice/share/Scripts/python/

    Это каталог по умолчанию, он может быть иным, если при установке был выбран иной каталог. Если каталог не существует, вы должны его создать с учётом заглавных символов.

    Внутри документа

      • Любой ODF файл - это в действительности ZIP-архив, который можно распаковать. В корне создайте каталог
        Scripts/python/
        и скопируйте внутрь любые файлы python, например
        mymacros.py
      • Вы должны увидеть.
    myfile
     |   ...
     ├── META-INF
     │   └── manifest.xml
     ├── Scripts
     │   └── python
     │       └── mymacros.py
     ...
    
    • Отредактируйте файл manifest.xml в каталоге META-INF и добавьте строки, просто до закрывающего тэга </manifest:manifest>
    • Any ODF file, really is a ZIP file, you can extract this file like extract normally this type files. In the root, create folder
      Scripts/python/
      and copy inside any python file, for example:
      mymacros.py
      • You should see
     myfile
     |   ...
     ├── META-INF
     │   └── manifest.xml
     ├── Scripts
     │   └── python
     │       └── mymacros.py
     ...
    
    • Edit file manifest.xml into folder META-INF and add lines, just before tag close </manifest:manifest>

    <manifest:file-entry manifest:full-path="Scripts/python/mymacros.py" manifest:media-type="" />
    <manifest:file-entry manifest:full-path="Scripts/python/" manifest:media-type="application/binary" />
    <manifest:file-entry manifest:full-path="Scripts/" manifest:media-type="application/binary" />

    • В итоге, файл должен выглядеть как:

    <?xml version="1.0" encoding="UTF-8"?>
      <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0">
        <manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/>
        <manifest:file-entry manifest:full-path="Thumbnails/thumbnail.png" manifest:media-type="image/png"/>
        <manifest:file-entry manifest:full-path="settings.xml" manifest:media-type="text/xml"/>
        <manifest:file-entry manifest:full-path="manifest.rdf" manifest:media-type="application/rdf+xml"/>
        <manifest:file-entry manifest:full-path="Configurations2/" manifest:media-type="application/vnd.sun.xml.ui.configuration"/>
        <manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/>
        <manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/>
        <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
        <manifest:file-entry manifest:full-path="Scripts/python/mymacros.py" manifest:media-type="" />
        <manifest:file-entry manifest:full-path="Scripts/python/" manifest:media-type="application/binary" />
        <manifest:file-entry manifest:full-path="Scripts/" manifest:media-type="application/binary" />
      </manifest:manifest>

    • Теперь, запакуйте каталог обратно. Внимание, не пакуйте внешний каталог, запакуйте только содержимое каталога.

    Return to Index