Class DrawForms

Introduction

The DrawForms class represents the collection of forms in a Draw document.

This class contains several python magic methods to make it behave like a collection.

Getting Number of Forms

To get the number of pages in a draw document, use the built in len() method:

>>> doc = DrawDoc(Draw.create_draw_doc())
>>> draw_page = doc.slides[0]
>>> len(draw_page.forms)
0
>>>draw_page.forms.add_form()
>>> len(draw_page.forms)
1

Getting a form

There are several ways to get a form from a Calc sheet. The simplest is to use the [] method:

Get Form by Index.

>>> doc = DrawDoc(Draw.create_draw_doc())
>>> draw_page = doc.slides[0]
>>> if len(draw_page.forms) == 0:
...   draw_page.forms.add_form()
>>> form = draw_page.forms[0]
<ooodev.draw.DrawForm object at 0x7f8b1c0b4a90>

Get Form by Name.

>>> doc = DrawDoc(Draw.create_draw_doc())
>>> draw_page = doc.slides[0]
>>> if len(draw_page.forms) == 0:
...   draw_page.forms.add_form("MyForm")
>>> form = draw_page.forms["MyForm"]
<ooodev.draw.DrawForm object at 0x7f8b1c0b4a90>

To get the last form in a sheet, use the -1 index:

>>> doc = DrawDoc(Draw.create_draw_doc())
>>> draw_page = doc.slides[0]
>>> if len(draw_page.forms) == 0:
...   draw_page.forms.add_form()
>>> form = draw_page.forms[-1]
<ooodev.draw.DrawForm object at 0x7f8b1c0b4a90>

Deleting a form

To delete a form, use the del keyword:

Delete by form index.

>>> del doc.slides[0].draw_page.forms[1]

Delete by form name.

>>> del doc.slides[0].draw_page.forms["MyForm"]

Iterating over forms

To iterate over the forms in a sheet, use the for keyword:

>>> for form in doc.slides[0].draw_page.forms:
...     print(form.name)
MyForm

An example can be found on Live LibreOffice Python UNO Examples. Example: Build Form.

Class Declaration

class ooodev.draw.DrawForms(owner, forms, lo_inst=None)[source]

Bases: LoInstPropsPartial, OfficeDocumentPropPartial, FormsComp, QiPartial, ServicePartial, TheDictionaryPartial

Class for managing Draw Forms.

This class is Enumerable and returns DrawForm instance on iteration.

for sheet in doc.sheets:
    sheet["A1"].set_val("test")
    assert sheet["A1"].get_val() == "test"

This class also as index access and returns CalcSheet instance.

sheet = doc.sheets["Sheet1"]
# or set the value of cell A2 to TEST
doc.sheets[0]["A2"].set_val("TEST")

# get the last sheet of the document
last_sheet = doc.sheets[-1]

# get the second last sheet of the document
second_last_sheet = doc.sheets[-2]

# get the number of sheets
num_sheets = len(doc.sheets)

New in version 0.18.3.

__init__(owner, forms, lo_inst=None)[source]

Constructor

Parameters:
  • owner (DrawPage) – Owner Component

  • forms (XForms) – Forms instance.

  • lo_inst (LoInst | None) –

Return type:

None

add_container_listener(listener)

Adds the specified listener to receive events when elements are inserted or removed.

Parameters:

listener (XContainerListener) – The listener to be added.

Return type:

None

add_event_listener(listener)

Adds an event listener to the component.

Parameters:

listener (XEventListener) – The event listener to be added.

Return type:

None

add_form()[source]
add_form(idx: int)
add_form(name: str)
add_form(*args, **kwargs)

Adds a new form.

Parameters:
  • name (str) – Name of form.

  • idx (int) – Index of form.

Raises:

NameClashError – If name already exists.

Returns:

Form

Return type:

DrawForm

create_clone()

Creates a clone of the object.

Returns:

The clone.

Return type:

XCloneable

create_enumeration()

Creates an enumeration of the container’s elements.

Return type:

XEnumeration

dispose()

Disposes the component.

Return type:

None

get_by_index(idx)[source]

Gets the element at the specified index.

Parameters:

idx (int) – The Zero-based index of the element. Idx can be a negative value to index from the end of the list. For example, -1 will return the last element.

Returns:

The element at the specified index.

Return type:

DrawForm

get_by_name(name)[source]

Gets the element with the specified name.

Parameters:

name (str) – The name of the element.

Raises:

MissingNameError – If sheet is not found.

Returns:

The element with the specified name.

Return type:

DrawForm

get_count()

Gets the number of elements contained in the container.

Returns:

The number of elements.

Return type:

int

get_element_names()

Gets the names of all elements contained in the container.

Returns:

The names of all elements.

Return type:

tuple[str, …]

get_element_type()

Gets the type of the elements contained in the container.

Returns:

The type of the elements. None means that it is a multi-type container and you cannot determine the exact types with this interface.

Return type:

Any

get_parent()

Returns the parent of the object.

Return type:

XInterface

get_services()

Gets service names for the instance.

Returns:

service names

Return type:

List[str]

has_by_name(name)

Checks if the container has an element with the specified name.

Parameters:

name (str) – The name of the element.

Returns:

True if the container has an element with the specified name, otherwise False.

Return type:

bool

has_elements()

Determines whether the container has elements.

Return type:

bool

insert_by_index(index, element)

Inserts the given element at the specified index.

To append an element, use the index last index +1.

Parameters:
  • index (int) – The Zero-based index at which the element should be inserted.

  • element (T) – The element to insert.

Raises:
  • IllegalArgumentExceptioncom.sun.star.lang.IllegalArgumentException

  • IndexOutOfBoundsExceptioncom.sun.star.lang.IndexOutOfBoundsException

  • WrappedTargetExceptioncom.sun.star.lang.WrappedTargetException

Return type:

None

insert_by_name(name, element)

Inserts the element with the specified name.

Parameters:
  • name (str) – The name of the element to be inserted.

  • element (T) – The new element.

Return type:

None

qi(atype, raise_err=False)

Generic method that get an interface instance from an object.

Parameters:
  • atype (T) – Interface type to query obj for. Any Uno class that starts with ‘X’ such as XInterface

  • raise_err (bool, optional) – If True then raises MissingInterfaceError if result is None. Default False

Raises:

MissingInterfaceError – If ‘raise_err’ is ‘True’ and result is None

Returns:

instance of interface if supported; Otherwise, None

Return type:

T | None

Note

When raise_err=True return value will never be None.

remove_by_index(index)

Removes the element at the specified index.

Parameters:

index (int) – The Zero-based index of the element to remove.

Raises:
  • IndexOutOfBoundsExceptioncom.sun.star.lang.IndexOutOfBoundsException

  • WrappedTargetExceptioncom.sun.star.lang.WrappedTargetException

Return type:

None

remove_by_name(name)

Removes the element with the specified name.

Parameters:

name (str) – The name of the element to be removed.

Return type:

None

remove_container_listener(listener)

Removes the specified listener so it does not receive any events from this container.

Parameters:

listener (XContainerListener) – The listener to be removed.

Return type:

None

remove_event_listener(listener)

Removes an event listener from the component.

Parameters:

listener (XEventListener) – The event listener to be removed.

Return type:

None

replace_by_index(index, element)

Replaces the element at the specified index with the given element.

Parameters:
  • index (int) – The index of the element that is to be replaced.

  • element (Any) – The replacement element.

Return type:

None

replace_by_name(name, element)

Replaces the element with the specified name.

Parameters:
  • name (str) – The name of the element to be replaced.

  • element (T) – The new element.

Return type:

None

set_parent(parent)

Sets the parent of the object.

Return type:

None

Parameters:

parent (com.sun.star.uno.XInterface) –

support_service(*service)

Gets if instance supports a service.

Parameters:

*service (str) – Variable length argument list of UNO namespace strings such as com.sun.star.configuration.GroupAccess

Returns:

True if instance supports any passed in service; Otherwise, False

Return type:

bool

property component: com.sun.star.form.Forms

Forms Component

Return type:

Forms

property extra_data: TheDict

Extra Data Key Value Pair Dictionary.

Properties can be assigned properties and access like a dictionary and with dot notation.

Note

This is a dictionary object that can be used to store key value pairs. Generally speaking this data is not part of the object’s main data structure and is not saved with the object (document).

This property is used to store data that is not part of the object’s main data structure and can be used however the developer sees fit.

Return type:

TheDict

property lo_inst: LoInst

Lo Instance

Return type:

LoInst

property office_doc: OfficeDocumentT

Office Document.

Return type:

OfficeDocumentT

property owner: DrawPage

Returns: DrawPage: Draw Form

Return type:

DrawPage