zaz/doc/build/_sources/main/application.rst.txt

511 lines
7.3 KiB
Plaintext
Raw Normal View History

2021-06-25 22:55:52 -05:00
Application
-----------
Remember, always import library.
.. code-block:: python
import easymacro as app
Create instances
^^^^^^^^^^^^^^^^
* Instances without context
.. code-block:: python
toolkit = app.create_instance("com.sun.star.awt.Toolkit")
* Instances with context
.. code-block:: python
service = 'com.sun.star.awt.DialogProvider2'
dialog = app.create_instance(service, True)
* Get desktop
.. code-block:: python
desktop1 = app.create_instance('com.sun.star.frame.Desktop', True)
# ~ or
desktop2 = app.get_desktop()
app.msgbox(desktop1 == desktop2)
Current doc
^^^^^^^^^^^
.. code-block:: python
doc = app.active
app.msgbox(doc.title)
Iter docs
^^^^^^^^^
.. code-block:: python
for doc in app.docs:
app.msgbox(doc.title)
Count
^^^^^
.. code-block:: python
count = len(app.docs)
app.msgbox(count)
Get by name
^^^^^^^^^^^
.. code-block:: python
name = 'MyDoc.ods'
if name in app.docs:
doc = app.docs[name]
app.msgbox(doc.title)
2021-06-30 22:26:08 -05:00
If contain
^^^^^^^^^^
.. code-block:: python
result = 'myfile.ods' in app.docs
app.msgbox(result)
2021-06-25 22:55:52 -05:00
New
^^^
For default create new Calc document.
.. code-block:: python
doc = app.docs.new()
app.msgbox(doc.type)
For new Writer document.
.. code-block:: python
doc = app.docs.new('writer')
app.msgbox(doc.type)
With arguments.
.. code-block:: python
args= {'Hidden': True}
doc = app.docs.new('writer', args)
msg = f'{doc.type} - {doc.title}'
app.msgbox(msg)
doc.visible = True
Other documents.
.. code-block:: python
doc = app.docs.new('draw')
app.msgbox(doc.type)
doc = app.docs.new('impress')
app.msgbox(doc.type)
Open
^^^^
.. code-block:: python
path = '/home/mau/MyDoc.ods'
doc = app.docs.open(path)
While LibreOffice support format, you can open arbitrary file.
.. code-block:: python
path = '/home/mau/example.xlsx'
doc = app.docs.open(path)
With arguments.
.. code-block:: python
path = '/home/mau/example.odt'
args = {'Password': 'letmein'}
doc = app.docs.open(path, args)
2021-06-29 22:01:10 -05:00
Save
^^^^
2021-06-30 22:26:08 -05:00
.. code-block:: python
path = '/home/mau/myfile.ods'
doc = app.docs.new()
doc.save(path)
* If previously open and modify any file.
.. code-block:: python
doc.save()
* Open exists file and save with other name.
.. code-block:: python
path = '/home/mau/myfile.ods'
doc = app.docs.open(path)
new_path = '/srv/mau/other_name.ods'
doc.save(new_path)
Close
^^^^^
.. code-block:: python
doc = app.docs.new()
app.msgbox(doc.title)
doc.close()
2021-06-29 22:01:10 -05:00
To PDF
^^^^^^
* Save in path
.. code-block:: python
doc = app.active
path = '/home/mau/test.pdf'
doc.to_pdf(path)
* Save in memory
.. code-block:: python
doc = app.active
pdf = doc.to_pdf()
Export
^^^^^^
2021-07-02 22:10:16 -05:00
* Export common formats
.. code-block:: python
doc = app.docs.new()
path = '/home/mau/myfile.xlsx'
filter_name = 'xlsx'
doc.export(path, filter_name)
path = '/home/mau/myfile.xls'
filter_name = 'xls'
doc.export(path, filter_name)
doc = app.docs.new('writer')
path = '/home/mau/myfile.docx'
filter_name = 'docx'
doc.export(path, filter_name)
path = '/home/mau/myfile.doc'
filter_name = 'doc'
doc.export(path, filter_name)
2021-07-03 17:32:51 -05:00
path = '/home/mau/myfile.rtf'
filter_name = 'rtf'
doc.export(path, filter_name)
2021-07-02 22:10:16 -05:00
* For all support formats look `Apendix`_
2021-07-03 17:32:51 -05:00
* Export in memory.
.. code-block:: python
doc = app.docs.new()
filter_name = 'xlsx'
excel_doc = doc.export(filter_name=filter_name)
2021-06-29 22:01:10 -05:00
Fonst
^^^^^
* Get all fonts
.. code-block:: python
fonts = app.get_fonts()
for f in fonts:
print(f'Name: {f.Name} - StyleName: {f.StyleName}')
2021-06-29 22:01:10 -05:00
2021-06-30 22:26:08 -05:00
Filters
^^^^^^^
* Get all `support filters`_
.. code-block:: python
filters = app.get_filters()
ds = []
for f in filters:
data = f"UI Name: {f['UIName']} - Name: {f['Name']} - Type: {f['Type']}"
app.debug(data)
2021-06-25 22:55:52 -05:00
Call dispatch
^^^^^^^^^^^^^
You can call any `dispatch command`_ used only if property or method no exists in original object or in `easymacro.py`
.. code-block:: python
doc = app.active
command = '.uno:Gallery'
app.call_dispatch(doc, command)
2021-06-30 22:26:08 -05:00
Properties
^^^^^^^^^^
2021-07-02 22:10:16 -05:00
obj
~~~
* Get original object pyUNO (read only)
.. code-block:: python
doc = app.active
app.msgbox(type(doc))
app.msgbox(type(doc.obj))
title
2021-06-30 22:26:08 -05:00
~~~~~
.. code-block:: python
doc = app.active
app.msgbox(doc.title)
2021-07-03 17:32:51 -05:00
doc.title = 'New title'
app.msgbox(doc.title)
2021-06-30 22:26:08 -05:00
2021-06-25 22:55:52 -05:00
2021-07-02 22:10:16 -05:00
type
~~~~
2021-07-03 17:32:51 -05:00
* Get type document: calc, writer, etc. (read only)
2021-07-02 22:10:16 -05:00
.. code-block:: python
doc = app.active
app.msgbox(doc.type)
uid
~~~
2021-07-03 17:32:51 -05:00
* Get internal RuntimeUID form document. (read only)
2021-07-02 22:10:16 -05:00
.. code-block:: python
doc = app.active
app.msgbox(doc.uid)
is_saved
~~~~~~~~
2021-07-03 17:32:51 -05:00
* If document is saved in this or not (read only)
2021-07-02 22:10:16 -05:00
.. code-block:: python
doc = app.active
app.msgbox(doc.is_saved)
is_modified
~~~~~~~~~~~
2021-07-03 17:32:51 -05:00
* If document has been modified (read only)
2021-07-02 22:10:16 -05:00
.. code-block:: python
doc = app.active
app.msgbox(doc.is_modified)
is_read_only
~~~~~~~~~~~~
.. code-block:: python
doc = app.active
app.msgbox(doc.is_read_only)
path
~~~~
2021-07-03 17:32:51 -05:00
* Get path of document. (read only)
2021-07-02 22:10:16 -05:00
.. code-block:: python
doc = app.active
app.msgbox(doc.path)
dir
2021-07-03 17:32:51 -05:00
~~~
2021-07-02 22:10:16 -05:00
2021-07-03 17:32:51 -05:00
* Get only directory from path saved (read only)
2021-07-02 22:10:16 -05:00
.. code-block:: python
doc = app.active
app.msgbox(doc.dir)
2021-07-03 17:32:51 -05:00
file_name
~~~~~~~~~
* Get only file name from path saved (read only)
.. code-block:: python
doc = app.active
app.msgbox(doc.file_name)
name
~~~~
* Get only name without extension (read only)
.. code-block:: python
doc = app.active
app.msgbox(doc.file_name)
visible
~~~~~~~
* Hide or show document.
.. code-block:: python
doc = app.active
doc.visible = False
app.msgbox(doc.visible)
doc.visible = True
zoom
~~~~
* Get or set zoom value.
.. code-block:: python
doc = app.active
zoom = doc.zoom
app.msgbox(zoom)
doc.zoom = zoom * 2
app.msgbox(doc.zoom)
doc.zoom = zoom
selection
~~~~~~~~~
* **CAUTION**: Selection can be many things.
.. code-block:: python
doc = app.active
selection = doc.selection
app.msgbox(selection)
status_bar
~~~~~~~~~~
2021-07-02 22:10:16 -05:00
2021-07-03 17:32:51 -05:00
* Get status bar, always control in other thread.
.. code-block:: python
@app.run_in_thread
def update_status_bar(sb, text, limit):
sb.start(text, limit)
for i in range(limit):
sb.setValue(i)
app.sleep(1)
# ~ Is important free status bar
sb.end()
return
def main():
doc = app.active
update_status_bar(doc.status_bar, 'Line', 10)
return
Methods
^^^^^^^
set_focus
~~~~~~~~~
.. code-block:: python
name = 'MyDoc.ods'
if name in app.docs:
doc = app.docs[name]
doc.set_focus()
copy
~~~~
* Copy current selection
.. code-block:: python
doc = app.active
doc.copy()
paste
~~~~~
* Paste any content in clipboard
.. code-block:: python
doc = app.active
doc.paste()
2021-07-02 22:10:16 -05:00
2021-06-25 22:55:52 -05:00
.. _dispatch command: https://wiki.documentfoundation.org/Development/DispatchCommands
2021-06-30 22:26:08 -05:00
.. _support filters: https://help.libreoffice.org/latest/en-US/text/shared/guide/convertfilters.html
2021-07-02 22:10:16 -05:00
.. _Apendix: apendixes.html