3 documents
Mauricio Baeza edited this page 2019-10-09 16:50:42 -05:00

Documents

Always import the library

import easymacro as app

IMPORTANT: Many objects in easymacro.py are custom classes, you always can return original pyUNO object with the property obj

New

  • Default type of document is calc
    doc = app.new_doc()
    app.msgbox(doc.type)
    app.mri(doc.obj)
  • New writer
    doc = app.new_doc('writer')
    app.msgbox(doc.type)
  • Open con arguments
    args = {'Hidden': True}
    doc = app.new_doc('draw', **args)
    app.msgbox(doc.type)
    doc.close()

For other documents: impress, math

  • For new Base document
    path = "/home/mau/dbtest.odb"
    db = app.new_db(path)
    app.msgbox(db.type)

Open

  • Path ODF documents
    path = '/home/mau/calc.ods'
    doc = app.open_doc(path)
  • Open with password
    path = '/home/mau/writer.odt'
    args = {'Password': 'letmein'}
    doc = app.open_doc(path, **args)
  • Open hidden, always close document if not used more.
    path = '/home/mau/draw.odg'
    args = {'Hidden': True}
    doc = app.open_doc(path, **args)
    app.msgbox(doc.title)
    doc.close()
  • Open like template, or False for open template for edit.
    path = '/home/mau/calc.ods'
    args = {'AsTemplate': True}
    doc = app.open_doc(path, **args)
  • Other common options
    args = {'ReadOnly': True}
    args = {'Preview': True}

    # For macros activate
    args = {'MacroExecutionMode': 4}

Get documents

    for doc in app.get_documents():
        app.msgbox(doc.title)

Get document

  • Current document
    doc = app.get_document()
    app.msgbox(doc.title)
  • Get by title
    doc = app.get_document('calc.ods')
    app.msgbox(doc.title)

Get type

  • Values return: calc, writer, impress, draw, math, base, ide
    doc = app.get_document()
    app.msgbox(doc.type)

Send focus

    doc = app.get_document('calc.ods')
    doc.focus()

Visible

  • Show or hidden any document
    doc = app.get_document('calc.ods')
    doc.visible = False
    app.msgbox(doc.title)
    doc.visible = True

Zoom

  • Set factor to zoom view
    doc = app.get_document()
    doc.zoom = 150

Selection

  • Get current selection
    doc = app.get_document()
    sel = doc.selection
    app.msgbox(sel.obj.ImplementationName)

Status bar

@app.run_in_thread
def update_status_bar(statusbar, text, limit):
    statusbar.start(text, limit)
    for i in range(limit):
        statusbar.setValue(i)
        app.sleep(1)
    # ~ Is important free status bar
    statusbar.end()
    return


def main():
    doc = app.get_document()
    statusbar = doc.statusbar
    update_status_bar(statusbar, 'Line', 10)
    return

Export to PDF

  • If export is correct, return path save PDF

Look more options for export in PDF export filter data

  • Automatic select path
    path = '/home/mau/calc.ods'
    args = {'Hidden': True}
    doc = app.open_doc(path, **args)
    # ~ Automatic save in path: /home/mau/calc.pdf
    path_pdf = doc.to_pdf('', **args)
    doc.close()
  • Save in other directory
    path = '/home/mau/calc.ods'
    args = {'Hidden': True}
    doc = app.open_doc(path, **args)

    # ~ Save in path: /home/mau/Desktop/test/calc.pdf
    path = "/home/mau/Desktop/test"
    path_pdf = doc.to_pdf(path, **args)
    doc.close()
  • Custom target
    path = '/home/mau/calc.ods'
    args = {'Hidden': True}
    doc = app.open_doc(path, **args)

    path = "/home/mau/Desktop/other_name.pdf"
    path_pdf = doc.to_pdf(path, **args)
    doc.close()
  • Set other options
    path = '/home/mau/writer.odt'
    args = {'Hidden': True}
    doc = app.open_doc(path, **args)

    args = {
        'PageRange': '2',
        'EncryptFile': True,
        'DocumentOpenPassword': 'letmein',
    }
    path_pdf = doc.to_pdf('', **args)
    doc.close()