
Documents
=========

Remember,  always import library.

.. code-block:: python

    import easymacro as app


Current doc
-----------

.. code-block:: python

    doc = app.docs.active
    app.msgbox(doc.title)


Iter docs
---------

.. code-block:: python

    for doc in app.docs:
        app.debug(doc.type, doc.title)


Count
-----

.. code-block:: python

    count = len(app.docs)
    app.debug(count)


Get by index
------------

.. code-block:: python

    doc = app.docs[1]
    app.debug(doc.type, doc.title)


Get by name
-----------

.. code-block:: python

    name = 'MyDoc.ods'
    if name in app.docs:
        doc = app.docs[name]
        app.debug(doc.type, doc.title)


If contain
----------

.. code-block:: python

    result = 'myfile.ods' in app.docs
    app.debug(result)


New
---

For default create new Calc document.

.. code-block:: python

    doc = app.docs.new()
    app.debug(doc.type)


For new Writer document.

.. code-block:: python

    doc = app.docs.new('writer')
    app.debug(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.debug(doc.type)

    doc = app.docs.new('impress')
    app.debug(doc.type)

    doc = app.docs.new('math')
    app.debug(doc.type)


Open
----

.. code-block:: python

    path = '/home/mau/ask_example.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.ods'
    args = {'Password': 'letmein'}
    doc = app.docs.open(path, args)


Save
----

* Save new documents

.. code-block:: python

    path = '/home/mau/myfile.ods'
    doc = app.docs.new()
    doc.save(path)

* If previously open and modify then.

.. 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 = '/home/mau/other_name.ods'
    doc.save(new_path)


Close
-----

.. code-block:: python

    doc = app.docs.new()
    app.msgbox(doc.title)
    doc.close()


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
------

* 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)

    path = '/home/mau/myfile.rtf'
    filter_name = 'rtf'
    doc.export(path, filter_name)


* Export in memory.

.. code-block:: python

    doc = app.docs.new()
    filter_name = 'xlsx'
    excel_doc = doc.export(filter_name=filter_name)


Properties
----------

Common properties for documents

obj
^^^

* Get original object pyUNO (read only)

.. code-block:: python

    doc = app.active
    app.debug(type(doc))
    app.debug(type(doc.obj))


title
^^^^^

.. code-block:: python

    doc = app.active
    app.debug(doc.title)
    doc.title = 'New title'
    app.debug(doc.title)


type
^^^^

* Get type document: calc, writer, etc. (read only)

.. code-block:: python

    doc = app.active
    app.debug(doc.type)


uid
^^^

* Get internal RuntimeUID form document. (read only)

.. code-block:: python

    doc = app.active
    app.debug(doc.uid)


is_saved
^^^^^^^^

* If document is saved or not (read only)

.. code-block:: python

    doc = app.active
    app.debug(doc.is_saved)


is_modified
^^^^^^^^^^^

* If document has been modified (read only)

.. code-block:: python

    doc = app.active
    app.debug(doc.is_modified)


is_read_only
^^^^^^^^^^^^

.. code-block:: python

    doc = app.active
    app.debug(doc.is_read_only)


path
^^^^

* Get path of document. (read only)

.. code-block:: python

    doc = app.active
    app.debug(doc.path)


dir
^^^

* Get only directory from path saved (read only)

.. code-block:: python

    doc = app.active
    app.debug(doc.dir)


file_name
^^^^^^^^^

* Get only file name from path saved (read only)

.. code-block:: python

    doc = app.active
    app.debug(doc.file_name)


name
^^^^

* Get only name without extension (read only)

.. code-block:: python

    doc = app.active
    app.debug(doc.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


status_bar
^^^^^^^^^^

* 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


selection
^^^^^^^^^

* **CAUTION**: Selection can be many things.

.. code-block:: python

    doc = app.active
    selection = doc.selection
    app.debug(selection)


Methods
-------

set_focus
^^^^^^^^^

.. code-block:: python

    for doc in app.docs:
        app.debug(doc.title)
        doc.set_focus()
        app.sleep(1)

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()


paste_special
^^^^^^^^^^^^^

* Show dialog box Paste Special

.. code-block:: python

    doc = app.active
    doc.paste_special()

paste_values
^^^^^^^^^^^^

* Paste only values

.. code-block:: python

    doc = app.active
    doc.paste_values()


clear_undo
^^^^^^^^^^

* Clear history undo

.. code-block:: python

    doc = app.active
    doc.clear_undo()

